<?php
class userAction extends teamAction
{
    public function _initialize()
    {
        parent::_initialize();
        if ($_GET['session']) {
            print_r(session_id());
            exit();
        }
        if (!$this->is_visitor_login()) {
            $this->redirect('passport/login');
        }

        if (D('user')->where(['id' => $this->get_visitor_id()])->count() == 0) {
            $this->visitor->logout();
        }
		$this->tgroup = [100=>'普通用户',1=>'总经销商',2=>'经销商',3=>'代理商',4=>'经纪人',5=>'全民经纪人'];
		$this->assign('tgroup', $this->tgroup);
		$this->user_tgroup = $this->visitor->get('tgroup');
    }
	
	/* 用户首页 */
    public function index()
    {
		$info = D('user')->field('topkey,topclass,tgroup,status,price')->find($this->get_visitor_id());
		$nums = D('user')->where(array('topkey'=>$info['topkey']))->count();
		
		$topkey = $info['topkey'];
		$where['u.topkey'] = $topkey;
		$where['o.lottery'] = array('IN',array(1,2,9));

		$stime = date('Y-m-d') . ' 04:00:00';
        $etime = date('Y-m-d') . ' 23:59:59';

		/* 统计 */
		$order['total'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($where)->sum('prices');
		$where['o.add_time'][] =  array('elt', $etime);
		$where['o.add_time'][] =  array('egt', $stime);
		$order['days'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($where)->sum('prices');

		$order['total'] = $order['total'] ? $order['total'] : 0.00;
		$order['days'] = $order['days'] ? $order['days'] : 0.00;

		$this->assign(compact('info','nums','order'));
        $this->display();
    }

   public function agent_review(){
	    $mod = D("agent_log_ext");
	        if (!empty($mod)) {
	            $id = $this->visitor->get('id');
				$tgroup   = D('user')->where(array('id' => $id))->getField('tgroup');
				$tele   = D('user')->where(array('id' => $id))->getField('tele');
				$username   = D('user')->where(array('id' => $id))->getField('username');

		$where['tgroup_1']=array('eq',$id,'or'); 
		$where['tgroup_2']=array('eq',$id,'or'); 
		$where['tgroup_3']=array('eq',$id,'or'); 
		$where['tgroup_4']=array('eq',$id,'or'); 
		$where['_logic'] = 'or'; 

		$map['_complex'] =$where;


		 if ($_GET['username']) {
		$realname= trim($_GET['username']);
		$map['username'] = array('like', '%' . $_GET['username'] . '%','AND');
		    
		}
		    
		     if ($_GET['tele']) {
		$tele= trim($_GET['tele']);
		$map['tele'] =array('eq',$tele,'AND');
		    
		}
		    
		     if ($_GET['type']) {
		$agent_status= trim($_GET['type']);
		$map['agent_status'] =array('eq',$agent_status,'AND');
		    
		}
		    
		      if ($_GET['type']=='') {
		unset($map['agent_status']);
		    
		}
		    
		$this->assign('search', array(
		'id' =>$id,
		'type' => $type,
		'username'   => $_GET['username'],
		 'tele'   => $_GET['tele'],
		));

	            $result = $this->_list($mod, $map);
	            $this->assign('page', $result['page']);
				$list = $result['list'];
				$this->assign('list', $list);
				$this->assign('tgroup', $tgroup);
	            $this->display();
				$this->assign('search',array('username'=>$username,'tele'=>$tele));
		}
	}

        
   public function agent_act(){
	$id=$_GET[id];
	$map[id]=$id;
	 $commission=$_GET[commission];
	D('user')->where($map)->save(array(
	'tgroup'=>5,
	 'commission'=>$commission,

	));
	unset($map);
	$map[uid]=$id;
	$res=    D('agent_log')->where($map)->save(array(
	'agent_status'=>2,

	));
	$res ? $this->successres("操作成功") : $this->faileres("操作失败!");

	}
        
        
         public function successres($message = "操作成功",$ext='0') {

        $index["status"] = 1;

        $index["message"] = $message;
        
        $index["ext"] = $ext;

        $res = json_encode($index);

        echo $res;
        exit();
    }

    public function faileres($message = "获取失败",$ext='0') {

        $index["status"] = 0;

        $index["message"] = $message;
        
        $index["ext"] = $ext;

        $res = json_encode($index);

        echo $res;
        exit();
    }
        
       function _list($model, $map = array(), $sort_by = '', $order_by = '', $field_list = '*', $pagesize = 20)
    {
        //排序
        $mod_pk = $model->getPk();
        if ($this->_request("sort", 'trim')) {
            $sort = $this->_request("sort", 'trim');
        }
        else if (!empty($sort_by)) {
            $sort = $sort_by;
        }
        else if ($this->sort) {
            $sort = $this->sort;
        }
        else {
            $sort = $mod_pk;
        }
        if ($this->_request("order", 'trim')) {
            $order = $this->_request("order", 'trim');
        }
        else if (!empty($order_by)) {
            $order = $order_by;
        }
        else if ($this->order) {
            $order = $this->order;
        }
        else {
            $order = 'DESC';
        }
        $result = array();
        //如果需要分页
        if ($pagesize) {
            $count = $model->where($map)->count($mod_pk);
            $pager = new Page($count, $pagesize);
        }
        $select = $model->field($field_list)->where($map)->order($sort . ' ' . $order);
        $this->list_relation && $select->relation(true);
        if ($pagesize) {
            $select->limit($pager->firstRow . ',' . $pager->listRows);
            $page           = $pager->show();
            $result['page'] = $page;
            $this->assign("page", $page);
        }
        $list           = $select->select();
        $result['list'] = $list;
        //$this->assign('list', $list);
        $this->assign('list_table', true);
        return $result;
    }

	/* 团队管理 */
	public function team()
    {

		$topkey = $this->visitor->get('topkey');
		$where['topkey'] = $topkey;
		$where['tgroup_'.$this->user_tgroup] = $this->uid;
		
	   if ($_GET['stime']) {
				$time_start = $_GET['stime'];
				$time_end = $_GET['etime'];
				$where['reg_time'] = array(array('egt', $time_start), array('elt', $time_end));
				}

		if ($_GET[tgroup]) {
			$where['tgroup'] = ($_GET[tgroup] == 100) ? 0 : $_GET[tgroup];
			$tgroup = $_GET[tgroup];
		}
		($tele = $this->_get('tele', 'trim','')) && $where['tele'] = array('like',$tele);


		$page_size = 20;
		$count = D('user')->where($where)->count();
		$pager = new Page($count,$page_size);
		$list = D('user')->where($where)->limit($pager->firstRow, $pager->listRows)->order('reg_time desc')->select();
		 $list1 = D('user')->where($where)->order('reg_time desc')->select();
                foreach($list1 as $key=>$val){
					$sum += $list1[$key]['price'];
                    
                }
                $this->assign('sum', $sum);
                foreach($list1 as $key=>$val){
			if(in_array($val['status'],[1])){
				$list1[$key]['status'] = '启用';
                        }
			else{
				$list1[$key]['status'] = '无效';
			}
			$list1[$key]['tgroup']=get_tgroup($val[tgroup]);
			$list1[$key]['tgroup_1_name'] = D('user')->where(['id'=>$val['tgroup_1']])->getField('username');
			$list1[$key]['tgroup_2_name'] = D('user')->where(['id'=>$val['tgroup_2']])->getField('username');
			$list1[$key]['tgroup_3_name'] = D('user')->where(['id'=>$val['tgroup_3']])->getField('username');
			$list1[$key]['tgroup_4_name'] = D('user')->where(['id'=>$val['tgroup_4']])->getField('username');
			$list1[$key]['tgroup_5_name'] = D('user')->where(['id'=>$val['tgroup_5']])->getField('username');

		}
                
                   if($this->_request('export') != ''){
			// 引入excel类文件，将订单用excel文件导出来
			Vendor('excelClass.excelclass');
			$excel = new excelClass();
			$excel->echoTeam('团队'.date('YmdHis').'.xls',$list1);
			exit;
		}

		$page = $pager->show();
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search',array('tgroup'=>$tgroup,'tele'=>$tele));


		/* 统计 */
		$stime = date('Y-m-d') . ' 04:00:00';
        $etime = date('Y-m-d') . ' 23:59:59';

		/**************************************************** 交易统计 start **************************************************************/
		$order_map['u.topkey'] = $topkey;
		$order_map['u.tgroup_'.$this->user_tgroup] = $this->uid;
		$order_map['o.lottery'] = array('IN',array(1,2,9));
		$order_map['o.type'] = 0;

		$order['total'] = $order['refund'] = $order['delivery'];

		//交易总额
//		$order_total_list = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($order_map)->field('o.lottery as lottery,o.lottery_total as lottery_total,o.prices as prices')->select();
//		foreach($order_total_list as $tval){
//			$order['total'] += ($tval['lottery'] == 2 ? $tval['lottery_total'] : $tval['prices']);
//		}
		$order['total'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($order_map)->sum('prices');

		$r_map = $d_map = $order_map;

		//退款总额
		$r_map['o.status'] = 9;
		$order_refund_list = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($r_map)->field('o.lottery as lottery,o.lottery_total as lottery_total,o.prices as prices')->select();
		foreach($order_refund_list as $rval){
			$order['refund'] += ($rval['lottery'] == 2 ? $rval['lottery_total'] : $rval['prices']);
		}
		//提货总额
		$d_map['o.status'] = array('IN',[2,3,4,5,6]);//待提货也加入统计(升级失败待提货加入统计)
		$order_delivery_list = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($d_map)->field('o.lottery as lottery,o.lottery_total as lottery_total,o.prices as prices')->select();
		foreach($order_delivery_list as $dval){
			$order['delivery'] += ($dval['lottery'] == 2 ? $dval['lottery_total'] : $dval['prices']);
		}

		$this->assign('order',$order);

		/**************************************************** 交易统计 end **************************************************************/


		/**************************************************** 充值、提现统计 start **************************************************************/
		$recharge_map['u.topkey'] = $topkey;
		$recharge_map['u.tgroup_'.$this->user_tgroup] = $this->uid;
		$recharge_map['ur.status'] = 1;

		//充值总额
		$recharge_map['ur.type'] = 1;
		$recharge['total'] = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');

		//提现总额
		$recharge_map['ur.type'] = 2;
		$cash['total'] = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');

		//我的.充值总额
		unset($recharge_map['u.tgroup_'.$this->user_tgroup]);
		$recharge_map['u.id'] = $this->visitor->info['id'];
		$recharge_map['ur.type'] = 1;
		$recharge_user['total'] = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');

		//我的.提现总额
		$recharge_map['ur.type'] = 2;
		$cash_user['total'] = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');

		//默认0
		$recharge['total'] = $recharge['total'] ? $recharge['total'] : 0.00;
		$cash['total'] = $cash['total'] ? $cash['total'] : 0.00;
		$recharge_user['total'] = $recharge_user['total'] ? $recharge_user['total'] : 0.00;
		$cash_user['total'] = $cash_user['total'] ? $cash_user['total'] : 0.00;

		$this->assign('recharge',$recharge);
		$this->assign('cash',$cash);
		$this->assign('recharge_user',$recharge_user);
		$this->assign('cash_user',$cash_user);
		/**************************************************** 充值、提现统计 end **************************************************************/
	          $this->assign('search', array(
	            'id' =>$id,
	            'type' => $type,
	            'tgroup'   => $_GET['tgroup'],
	             'tele'   => $_GET['tele'],
	            'stime'   => $_GET['stime'],
	            'etime'   => $_GET['etime'],
	        ));
		$this->display();
    }

	/* 消费明细 */
	public function order(){
		$topkey = $this->visitor->get('topkey');

		/**************************************************** 订单列表 start **************************************************************/
		$map['u.topkey'] = $topkey;
		$map['u.tgroup_'.$this->user_tgroup] = $this->uid;
		$map['o.lottery'] = array('IN',array(1,2,9));

		$map['o.type'] = 0;
		($id = $this->_get('id', 'intval',0)) && $map['u.id'] = $id;
		($lottery = $this->_get('lottery', 'intval',0)) && $map['o.lottery'] = $lottery;
		($tele = $this->_get('tele', 'trim','')) && $map['u.tele'] = array('like','%'.$tele.'%');

		($map_stime = $this->_request('stime', 'trim')) && $map['o.add_time'][] = array('egt', $map_stime);
        ($map_etime = $this->_request('etime', 'trim')) && $map['o.add_time'][] = array('elt', $map_etime);


		($tgroup_1 = $this->_get('tgroup_1', 'intval',0)) && $map['u.tgroup_1'] = $tgroup_1;
		($tgroup_2 = $this->_get('tgroup_2', 'intval',0)) && $map['u.tgroup_2'] = $tgroup_2;
		($tgroup_3 = $this->_get('tgroup_3', 'intval',0)) && $map['u.tgroup_3'] = $tgroup_3;
		($tgroup_4 = $this->_get('tgroup_4', 'intval',0)) && $map['u.tgroup_4'] = $tgroup_4;

		
		$type = $this->_get('type', 'intval',0);
		if($type == 1){
			$map['o.status'] = array('IN',[1,2]);
		}elseif($type == 2){
			$map['o.status'] = array('IN',[9]);
		}elseif($type == 3){
			$map['o.status'] = array('IN',[3,4,5,6]);
		}

		$page_size = 20;
		$count = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($map)->count();
		$pager = new Page($count,$page_size);
		if($this->_request('export') != ''){
			$list = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->limit('0,1000')->where($map)->field('o.prices as prices,o.lottery_total as lottery_total,o.status as status,o.add_time as add_time,o.lottery as lottery,u.id as uid,u.username as username,u.tele as tele,o.id as oid,u.tgroup_1 as tgroup_1,u.tgroup_2 as tgroup_2,u.tgroup_3 as tgroup_3,u.tgroup_4 as tgroup_4')->order('o.add_time desc')->select();
		}else{
			$list = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->limit($pager->firstRow, $pager->listRows)->where($map)->field('o.prices as prices,o.lottery_total as lottery_total,o.status as status,o.add_time as add_time,o.lottery as lottery,u.id as uid,u.username as username,u.tele as tele,o.id as oid,o.orderid as orderid,u.tgroup_1 as tgroup_1,u.tgroup_2 as tgroup_2,u.tgroup_3 as tgroup_3,u.tgroup_4 as tgroup_4')->order('o.add_time desc')->select();
		}
		$lottery_arr = array(0=>'待升级',1=>'升级中',2=>'升级成功',9=>'升级失败');
		foreach($list as $key=>$val){

			if(in_array($val['status'],[3,4,5,6])){
				$list[$key]['status'] = '已提货';
			}
			if(in_array($val['lottery'],[0,1])){
				$list[$key]['status'] = '升级中';
			}
			elseif(in_array($val['status'],[9])){
				$list[$key]['status'] = '退款';
			}else{
				//$list[$key]['status'] = '待提货';
				$list[$key]['status'] = '已提货';
			}
//			if($val['lottery'] == 2){
//				$list[$key]['prices'] = $val['lottery_total'];
//			}
			$list[$key]['lottery'] = $lottery_arr[$val['lottery']];

			$item = D('order_item')->alias('oi')->join(table('item').' as i ON oi.item_id = i.id')->field('i.title as title,i.img as img,oi.nums as nums,oi.skus as skus')->where(array('oi.order_id'=>$val['oid']))->find();
			$list[$key]['title'] = $item['title'];
			$list[$key]['img'] = $item['img'];
			$list[$key]['nums'] = $item['nums'];
			$list[$key]['skus'] = $item['skus'];

			$list[$key]['tgroup_1_name'] = D('user')->where(['id'=>$val['tgroup_1']])->getField('username');
			$list[$key]['tgroup_2_name'] = D('user')->where(['id'=>$val['tgroup_2']])->getField('username');
			$list[$key]['tgroup_3_name'] = D('user')->where(['id'=>$val['tgroup_3']])->getField('username');
			$list[$key]['tgroup_4_name'] = D('user')->where(['id'=>$val['tgroup_4']])->getField('username');
			$list[$key]['tgroup_5_name'] = D('user')->where(['id'=>$val['tgroup_5']])->getField('username');

		}
		if($this->_request('export') != ''){
			// 引入excel类文件，将订单用excel文件导出来
			Vendor('excelClass.excelclass');
			$excel = new excelClass();
			$excel->echoUserOrderFile('订单'.date('YmdHis').'.xls',$list);
			exit;
		}
		$page = $pager->show();
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search',array('id'=>$id,'type'=>$type,'lottery'=>$lottery,'stime'=>$map_stime,'etime'=>$map_etime,'tele'=>$tele));
		/**************************************************** 订单列表 end **************************************************************/


		/**************************************************** 统计 start **************************************************************/
		$stime = date('Y-m-d') . ' 04:00:00';
        $etime = date('Y-m-d') . ' 23:59:59';
		$order_map['u.topkey'] = $topkey;
		$order_map['u.tgroup_'.$this->user_tgroup] = $this->uid;
		$order_map['o.lottery'] = array('IN',array(1,2,9));
		$order_map['o.type'] = 0;
		($id = $this->_get('id', 'intval',0)) && $order_map['u.id'] = $id;
		($lottery = $this->_get('lottery', 'intval',0)) && $order_map['o.lottery'] = $lottery;
		($tele = $this->_get('tele', 'trim','')) && $order_map['u.tele'] = array('like','%'.$tele.'%');
		($map_stime = $this->_request('stime', 'trim')) && $order_map['o.add_time'][] = array('egt', $map_stime);
        ($map_etime = $this->_request('etime', 'trim')) && $order_map['o.add_time'][] = array('elt', $map_etime);
		($tgroup_1 = $this->_get('tgroup_1', 'intval',0)) && $order_map['u.tgroup_1'] = $tgroup_1;
		($tgroup_2 = $this->_get('tgroup_2', 'intval',0)) && $order_map['u.tgroup_2'] = $tgroup_2;
		($tgroup_3 = $this->_get('tgroup_3', 'intval',0)) && $order_map['u.tgroup_3'] = $tgroup_3;
		($tgroup_4 = $this->_get('tgroup_4', 'intval',0)) && $order_map['u.tgroup_4'] = $tgroup_4;


		//订单总金额
		$order['total'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($order_map)->sum('prices');
		$arr=  D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->field('u.id,u.weixin')->where($order_map)->order('u.id desc')->select();
		//升级成功退款金额
		$s_r_map = $s_d_map = $s_w_map = $d_map = $up_map = $order_map;
		$s_r_map['o.status'] = array('IN',[8,9]);
		$s_r_map['o.lottery'] = 2;
		$order['success_refund'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($s_r_map)->sum('lottery_total');
		//升级成功提货金额
		$s_d_map['o.status'] = array('IN',[3,4,5]);
		$s_d_map['o.lottery'] = 2;
		$order['success_delivery'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($s_d_map)->sum('lottery_total');
		//升级成功待处理金额
		$s_w_map['o.status'] = 2;
		$s_w_map['o.lottery'] = 2;
		$order['success_wait'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($s_w_map)->sum('lottery_total');
		//升级失败金额
		$d_map['o.lottery'] = 9;
		$order['failed'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($d_map)->sum('prices');
		//升级中金额
		$up_map['o.lottery'] = 1;
		$order['uporder'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($up_map)->sum('prices');

		$order_map['o.add_time'][] =  array('elt', $etime);
		$order_map['o.add_time'][] =  array('egt', $stime);
		$order['days'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($order_map)->sum('prices');

		$order['total'] = $order['total'] ? $order['total'] : 0.00;
		$order['success_refund'] = $order['success_refund'] ? $order['success_refund'] : 0.00;
		$order['success_delivery'] = $order['success_delivery'] ? $order['success_delivery'] : 0.00;
		$order['success_wait'] = $order['success_wait'] ? $order['success_wait'] : 0.00;
		$order['failed'] = $order['failed'] ? $order['failed'] : 0.00;
		$order['uporder'] = $order['uporder'] ? $order['uporder'] : 0.00;
		$order['days'] = $order['days'] ? $order['days'] : 0.00;

		$this->assign('order',$order);
		$arr1=[];
		foreach ($arr as $key => $val) {
			$arr1[$key] = $val[id];
		}
		$arr1 = (!empty($arr)) ? $arr1 : Array(0) ;
		$kid=array_unique($arr1);


		$map_['uid'] = array('IN',$kid);
		$map_['action'] = 'commission';

		($map_stime = $this->_request('stime', 'trim')) && $map_['add_time'][] = array('egt', $map_stime);
		($map_etime = $this->_request('etime', 'trim')) && $map_['add_time'][] = array('elt', $map_etime);
		$commission = D('price_log')->where($map_)->sum('price');
		$commission = $commission ? $commission : 0.00;
		$this->assign('commission',$commission);
		$map_['action'] = 'express';
		$express = D('price_log')->where($map_)->sum('price');
		$this->assign('express',$express);


		/**************************************************** 统计 end **************************************************************/

		$this->display();
	}


	/* 订单详情 */
	public function order_detail(){
		$id = $this->_get('id','intval');
		$topkey = $this->visitor->get('topkey');
		$map['o.id'] = $id;
		$map['u.topkey'] = $topkey;

		$info = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->field('o.*')->where($map)->find();
		$item = D('order_item')->alias('oi')->join(table('item').' as i ON oi.item_id = i.id')->field('i.title as title,i.img as img,i.price as price,i.title_up as title_up,i.img_up as img_up,i.price_up as price_up,oi.nums as nums,oi.skus as skus')->where(array('oi.order_id'=>$info['id']))->find();
		$info['item'] = $item;
		$lottery = array(0=>'待升级',1=>'升级中',2=>'升级成功',9=>'升级失败');
		if(in_array($info['status'],[3,4,5,6])){
			$info['status_str'] = '已提货';
		}elseif(in_array($info['status'],[9])){
			$info['status_str'] = '退款';
		}else{
			//$info['status_str'] = '待提货';
			$info['status_str'] = '已提货';
		}
		$info['lottery_str'] = $lottery[$info['lottery']];
		if($info['lottery'] == 2){
			$info['total'] = $info['lottery_total'];
		}
		$this->assign('info',$info);
		$status = 1;
		if($info['lottery'] > 0 && !in_array($info['status'],[2,3,4,5,6])){
			$status = 2;
		}elseif($info['status'] == 3 || $info['status'] == 4){
			$status = 3;
		}elseif($info['status'] == 5 || $info['status'] == 6){
			$status = 4;
		}
		$this->assign('status',$status);
		$this->display();
	}

	/* 资金明细 */
	public function price(){
		$topkey = $this->visitor->get('topkey');
		$map['u.topkey'] = $topkey;
		$map['u.tgroup_'.$this->user_tgroup] = $this->uid;

		($id = $this->_get('id', 'intval',0)) && $map['u.id'] = $id;
		($type = $this->_get('type', 'intval',0)) && $map['ur.type'] = $type;

		($tgroup_1 = $this->_get('tgroup_1', 'intval',0)) && $map['u.tgroup_1'] = $tgroup_1;
		($tgroup_2 = $this->_get('tgroup_2', 'intval',0)) && $map['u.tgroup_2'] = $tgroup_2;
		($tgroup_3 = $this->_get('tgroup_3', 'intval',0)) && $map['u.tgroup_3'] = $tgroup_3;
		($tgroup_4 = $this->_get('tgroup_4', 'intval',0)) && $map['u.tgroup_4'] = $tgroup_4;

		if ($_GET['username']) {
		$realname= trim($_GET['username']);
		$map['u.username'] = array('like', '%' . $_GET['username'] . '%');
		$this->assign('username_',$username);

		}

		if ($_GET['tele']) {
		$tele= trim($_GET['tele']);
		$map['u.tele'] =$tele;
		$this->assign('tele_',$tele);

		}

		if ($_GET['stime']) {
		$time_start = $_GET['stime'];
		$time_end = $_GET['etime'];
		$map['ur.add_time'] = array(array('egt', $time_start), array('elt', $time_end));
		}

		$map['ur.status'] = 1;
		$count = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($map)->count();
		$page_size = 20;
		$pager = new Page($count,$page_size);
		if($this->_request('export') != ''){
			$list = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->field('ur.*,u.tele as tele,u.tgroup_1 as tgroup_1,u.tgroup_2 as tgroup_2,u.tgroup_3 as tgroup_3,u.tgroup_4 as tgroup_4')->where($map)->select();
			foreach($list as $key=>$val){
				$list[$key]['type'] = $val['type'] == 1 ? '充值' : '提现';
			}
		}else{
			$list = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->field('ur.*,u.tele as tele,u.tgroup_1 as tgroup_1,u.tgroup_2 as tgroup_2,u.tgroup_3 as tgroup_3,u.tgroup_4 as tgroup_4')->limit($pager->firstRow, $pager->listRows)->where($map)->select();
		}
		foreach($list as $key=>$val){
			$list[$key]['tgroup_1_name'] = D('user')->where(['id'=>$val['tgroup_1']])->getField('username');
			$list[$key]['tgroup_2_name'] = D('user')->where(['id'=>$val['tgroup_2']])->getField('username');
			$list[$key]['tgroup_3_name'] = D('user')->where(['id'=>$val['tgroup_3']])->getField('username');
			$list[$key]['tgroup_4_name'] = D('user')->where(['id'=>$val['tgroup_4']])->getField('username');
			$list[$key]['tgroup_5_name'] = D('user')->where(['id'=>$val['tgroup_5']])->getField('username');
		}
		$page = $pager->show();
		if($this->_request('export') != ''){
			// 引入excel类文件，将订单用excel文件导出来
			Vendor('excelClass.excelclass');
			$excel = new excelClass();
			$excel->echoUserPriceFile('资金'.date('YmdHis').'.xls',$list);
			exit;
		}
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search', array(
		'id' =>$id,
		'type' => $type,
		'tele'   => $_GET['tele'],
		'username'   => $_GET['username'],
		'stime'   => $_GET['stime'],
		'etime'   => $_GET['etime'],
		));


		/* 统计 */
		$stime = date('Y-m-d') . ' 04:00:00';
        $etime = date('Y-m-d') . ' 23:59:59';
		$recharge_map['u.topkey'] = $topkey;
		($id = $this->_get('id', 'intval',0)) && $recharge_map['u.id'] = $id;
		$recharge_map['u.tgroup_'.$this->user_tgroup] = $this->uid;

		($tgroup_1 = $this->_get('tgroup_1', 'intval',0)) && $recharge_map['u.tgroup_1'] = $tgroup_1;
		($tgroup_2 = $this->_get('tgroup_2', 'intval',0)) && $recharge_map['u.tgroup_2'] = $tgroup_2;
		($tgroup_3 = $this->_get('tgroup_3', 'intval',0)) && $recharge_map['u.tgroup_3'] = $tgroup_3;
		($tgroup_4 = $this->_get('tgroup_4', 'intval',0)) && $recharge_map['u.tgroup_4'] = $tgroup_4;

		$recharge_map['ur.status'] = 1;
		$recharge_map['ur.type'] = 1;
		$recharge['total'] = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');
		$recharge_map['ur.add_time'][] =  array('elt', $etime);
		$recharge_map['ur.add_time'][] =  array('egt', $stime);
		$recharge['days'] = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');

		$recharge_map['ur.type'] = 2;
		unset($recharge_map['ur.add_time']);
		$cash['total'] = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');
		$recharge_map['ur.add_time'][] =  array('elt', $etime);
		$recharge_map['ur.add_time'][] =  array('egt', $stime);
		$cash['days'] = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');

		$recharge['total'] = $recharge['total'] ? $recharge['total'] : 0.00;
		$recharge['days'] = $recharge['days'] ? $recharge['days'] : 0.00;
		$cash['total'] = $cash['total'] ? $cash['total'] : 0.00;
		$cash['days'] = $cash['days'] ? $cash['days'] : 0.00;

		$this->assign('recharge',$recharge);
		$this->assign('cash',$cash);

		$this->display();
	}

    /*
 * 订单明细
 */
    public function order_info()
    {
        //自己的团队
        $where = [];
        //自己的级别
        $selfTgroup = $this->visitor->info['tgroup'];
        //自己的id
        $selfUid = $this->visitor->info['id'];
        //三级联动
        if ($_GET['uid'])
        {
            //获取查询的uid 和级别
            $uid = $_GET['uid'];
            $tmp = M('user')
                ->field('tgroup,topkey')
                ->where(['id'=>$uid])
                ->find();
            $tgroup = $tmp['tgroup'];
            $topkey = $tmp['topkey'];
        }else{
            //获取查询自己的uid 和级别
            $uid = $selfUid;
            $tgroup = $selfTgroup;
            $topkey = $this->visitor->get('topkey');
        }

        //显示团队名
        $tmp_add_arr = [0=>'用户名',1=>'总经销商',2=>'经销商',3=>'代理商',4=>'经纪人',5=>'全民经纪人'];
        $tuanNmae = $tmp_add_arr[$tgroup];
        $this->assign('tuanNmae',$tuanNmae);

        //查询团队列表
        $listTeams = getTeamList($selfTgroup,$selfUid);
        $this->assign('listTeams',$listTeams);

        //获取用户昵称
        $new_username = $this->getUserNmae($uid);
        $this->assign('uid',$uid);
        $this->assign('new_username',$new_username);

        //判断是否最后一个
        $tmp_whe["tgroup_".$tgroup] = $uid;
        $info_where = [];
        $tmp_if = M('user')
            ->field('id')
            ->where($tmp_whe)
            ->find();
        if (empty($tmp_if))
        {
            $where["u.id"] = $uid;
        }else{
            $where["u.tgroup_".$tgroup] = $uid;
        }

        //查询用户账号
        if (!empty($_GET['tele']))
        {
            $where['u.tele'] = array('LIKE','%'.$_GET['tele'].'%');
        }

        //查询用户昵称
        if (!empty($_GET['username']))
        {
            $where['u.username'] = array('LIKE','%'.$_GET['username'].'%');
        }

        //查询开始时间
        if (!empty($_GET['stime']))
        {
            $where['o.add_time'][] = array('EGT',$_GET['stime']);
        }

        //查询结束时间
        if (!empty($_GET['etime']))
        {
            $where['o.add_time'][] = array('ELT',$_GET['etime']);
        }

        //升级状态
        $listLotteryStatus = [
            1=>'未升级',
            '升级中',
            '升级成功',
            10=>'升级失败'
        ];
        $this->assign('listLotteryStatus',$listLotteryStatus);
        if ($_GET['lottery'])
        {
            $where['o.lottery'] = $_GET['lottery']-1;
        }

        //查询订单状态
        $listOrderStatus = [
            1=>'代发货',
            2=>'已发货',
            5=>'已完成',
            7=>'已退款',
        ];
        $this->assign('listOrderStatus',$listOrderStatus);
        if (!empty($_GET['status']))
        {
            $where['o.status'] = $_GET['status'];
        }

        //f分页设置
        $model_order = M('order');
        $page_size = 20;
        $count = $model_order->alias('o')
            ->join('ins_user as u ON u.id=o.uid')
//            ->field('u.username,u.tele,u.tgroup_4,u.tgroup_5,o.prices,o.lottery,o.status,u.tgroup_3,u.tgroup_2,u.tgroup_1,u.topkey')
            ->field('o.total,o.lottery_total,o.lottery,o.status')
            ->where($where)
            ->count();
        $pager = new Page($count,$page_size);
        /*
         *统计 start
         * 别问我 别的程序员没有备注
         * 不是我写的
         * 筛选条件不确定，所以直接引用自本类中的已完成方法
         * 能用 别问我为什么
         * 可以搜索一下查看源代码
         **/
        $stime = date('Y-m-d') . ' 04:00:00';
        $etime = date('Y-m-d') . ' 23:59:59';
        $order_map['u.topkey'] = $topkey;
        $order_map['u.tgroup_'.$tgroup] = $uid;
        $order_map['o.lottery'] = array('IN',array(1,2,9));
        $order_map['o.type'] = 0;
        ($id = $this->_get('id', 'intval',0)) && $order_map['u.id'] = $id;
        ($lottery = $this->_get('lottery', 'intval',0)) && $order_map['o.lottery'] = $lottery;
        ($tele = $this->_get('tele', 'trim','')) && $order_map['u.tele'] = array('like','%'.$tele.'%');
        ($map_stime = $this->_request('stime', 'trim')) && $order_map['o.add_time'][] = array('egt', $map_stime);
        ($map_etime = $this->_request('etime', 'trim')) && $order_map['o.add_time'][] = array('elt', $map_etime);
        ($tgroup_1 = $this->_get('tgroup_1', 'intval',0)) && $order_map['u.tgroup_1'] = $tgroup_1;
        ($tgroup_2 = $this->_get('tgroup_2', 'intval',0)) && $order_map['u.tgroup_2'] = $tgroup_2;
        ($tgroup_3 = $this->_get('tgroup_3', 'intval',0)) && $order_map['u.tgroup_3'] = $tgroup_3;
        ($tgroup_4 = $this->_get('tgroup_4', 'intval',0)) && $order_map['u.tgroup_4'] = $tgroup_4;

        //订单总金额
        $order['total'] = D('order')
            ->alias('o')
            ->join(table('user').' as u ON o.uid = u.id')
            ->where($order_map)
            ->sum('prices');
        $arr=  D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->field('u.id,u.weixin')->where($order_map)->order('u.id desc')->select();
        //升级成功退款金额
        $s_r_map = $s_d_map = $s_w_map = $d_map = $up_map = $order_map;
        $s_r_map['o.status'] = array('IN',[8,9]);
        $s_r_map['o.lottery'] = 2;
        $order['success_refund'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($s_r_map)->sum('lottery_total');
        //升级成功提货金额
        $s_d_map['o.status'] = array('IN',[3,4,5]);
        $s_d_map['o.lottery'] = 2;
        $order['success_delivery'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($s_d_map)->sum('lottery_total');
        //升级成功待处理金额统计
        $s_w_map['o.status'] = 2;
        $s_w_map['o.lottery'] = 2;
        $order['success_wait'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($s_w_map)->sum('lottery_total');
        //升级失败金额
        $d_map['o.lottery'] = 9;
        $order['failed'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($d_map)->sum('prices');
        //升级中金额
        $up_map['o.lottery'] = 1;
        $order['uporder'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($up_map)->sum('prices');

        $order_map['o.add_time'][] =  array('elt', $etime);
        $order_map['o.add_time'][] =  array('egt', $stime);
        $order['days'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($order_map)->sum('prices');

        $order['total'] = $order['total'] ? $order['total'] : 0.00;
        $order['success_refund'] = $order['success_refund'] ? $order['success_refund'] : 0.00;
        $order['success_delivery'] = $order['success_delivery'] ? $order['success_delivery'] : 0.00;
        $order['success_wait'] = $order['success_wait'] ? $order['success_wait'] : 0.00;
        $order['failed'] = $order['failed'] ? $order['failed'] : 0.00;
        $order['uporder'] = $order['uporder'] ? $order['uporder'] : 0.00;
        $order['days'] = $order['days'] ? $order['days'] : 0.00;

        $this->assign('order',$order);
        $arr1=[];
        foreach ($arr as $key => $val) {
            $arr1[$key] = $val[id];
        }
        $arr1 = (!empty($arr)) ? $arr1 : Array(0) ;
        $kid=array_unique($arr1);


        $map_['uid'] = array('IN',$kid);
        $map_['action'] = 'commission';

        ($map_stime = $this->_request('stime', 'trim')) && $map_['add_time'][] = array('egt', $map_stime);
        ($map_etime = $this->_request('etime', 'trim')) && $map_['add_time'][] = array('elt', $map_etime);
        $commission = D('price_log')->where($map_)->sum('price');
        $commission = $commission ? $commission : 0.00;
        $this->assign('commission',$commission);
        $map_['action'] = 'express';
        $express = D('price_log')->where($map_)->sum('price');
        $this->assign('express',$express);
        /**************************************************** 统计 end **************************************************************/

        //查询订单 联合用户表
        $lists = $model_order->alias('o')
            ->join('ins_user as u ON u.id=o.uid')
            ->field('u.username,u.tele,u.tgroup_4,u.tgroup_5,o.prices,o.lottery,o.status,u.tgroup_3,u.tgroup_2,u.tgroup_1,u.topkey')
            ->where($where)
            ->limit($pager->firstRow,$pager->listRows)
            ->select();
        $page = $pager->show();
        $this->assign('page',$page);
        // 导出执行的查询
        if($this->_request('export') != ''){
            $list1 = $model_order->alias('o')
                ->join('ins_user as u ON u.id=o.uid')
                ->field('u.username,u.tele,u.tgroup_4,u.tgroup_5,o.prices,o.lottery,o.status,u.tgroup_3,u.tgroup_2,u.tgroup_1,u.topkey')
                ->where($where)
                ->select();

            //处理所有
            $data = [];
            foreach ($list1 as $k=>$valu)
            {
                $valu['tgroup_4'] = $this->getUserNmae($valu['tgroup_4']);
                $valu['tgroup_5'] = $this->getUserNmae($valu['tgroup_5']);
                $valu['tgroup_3'] = $this->getUserNmae($valu['tgroup_3']);
                $valu['tgroup_2'] = $this->getUserNmae($valu['tgroup_2']);
                $valu['tgroup_1'] = $this->getUserNmae($valu['tgroup_1']);
                $valu['status'] = $listOrderStatus[$valu['status']];
                $valu['lottery'] = $listLotteryStatus[$valu['lottery']];
                $data[$k] = $valu;
            }

            Vendor('excelClass.excelclass');
            $excel = new excelClass();
            $excel->echoOrderInfo('订单明细'.date('YmdHis').'.xls',$data);
            exit;
        }

        //处理所有
        $data = [];
        foreach ($lists as $k=>$value)
        {
            $value['tgroup_4'] = $this->getUserNmae($value['tgroup_4']);
            $value['tgroup_5'] = $this->getUserNmae($value['tgroup_5']);
            $value['tgroup_3'] = $this->getUserNmae($value['tgroup_3']);
            $value['tgroup_2'] = $this->getUserNmae($value['tgroup_2']);
            $value['tgroup_1'] = $this->getUserNmae($value['tgroup_1']);
//            $tmp = $value['tgroup_1'];
            $value['status'] = $listOrderStatus[$value['status']];
            $value['lottery'] = $listLotteryStatus[$value['lottery']];
            $data[$k] = $value;
        }
        //分配查询列表
        $this->assign('lists',$data);
        $this->display();
    }

    //ajax获取联动
    //
    public function getNestUids()
    {
        //获取查询的uid 和级别

        $uid = $_GET['uid'];
        $tmp = M('user')->field('tgroup')->where(['id'=>$uid])->find();

        //如果有
        $tgroup = $tmp['tgroup'];
        $listTeams = getTeamList($tgroup,$uid);
        //处理成字符串
        $str = '<span class="standard_select fl">
<span class="select_shelter">
<select id="" name="uid" onchange="getNestUids(this)" class="order_select fl" style="width:112px;">';

        //判断是否最后
        if (empty($listTeams))
        {
            echo json_encode('');
        }else{
            $str .= '<option value="'.$uid.'">请选择</option>';
            foreach ($listTeams as $k=>$v)
            {
                $str .= '<option value="'.$v['id'].'">'.$v['username'].'</option>';
            }
            $str .= '</select></span></span>';
            echo json_encode($str);
        }

    }

    /*
* 返佣明细
*/
    public function rebate_info()
    {
        //自己的团队
        $where = [];
        //自己的级别
        $selfTgroup = $this->visitor->info['tgroup'];
        //自己的id
        $selfUid = $this->visitor->info['id'];
        //三级联动
        if ($_GET['uid'])
        {
            //获取查询的uid 和级别
            $uid = $_GET['uid'];
            $tmp = M('user')
                ->field('tgroup,topkey')
                ->where(['id'=>$uid])
                ->find();
            $tgroup = $tmp['tgroup'];
            $topkey = $tmp['topkey'];
        }else{
            //获取查询自己的uid 和级别
            $uid = $selfUid;
            $tgroup = $selfTgroup;
            $topkey = $this->visitor->get('topkey');
        }

        //显示团队名
        $tmp_add_arr = [0=>'用户名',1=>'总经销商',2=>'经销商',3=>'代理商',4=>'经纪人',5=>'全民经纪人'];
        $tuanNmae = $tmp_add_arr[$tgroup];
        $this->assign('tuanNmae',$tuanNmae);

        //查询团队列表
        $listTeams = getTeamList($selfTgroup,$selfUid);
        $this->assign('listTeams',$listTeams);

        //获取用户昵称
        $new_username = $this->getUserNmae($uid);
        $this->assign('uid',$uid);
        $this->assign('new_username',$new_username);

        //判断是否最后一个
        $tmp_whe["tgroup_".$tgroup] = $uid;
        $info_where = [];
        $tmp_if = M('user')
            ->field('id')
            ->where($tmp_whe)
            ->find();
        if (empty($tmp_if))
        {
            $where["u.id"] = $uid;
        }else{
            $where["u.tgroup_".$tgroup] = $uid;
        }

        //查询用户账号
        if (!empty($_GET['tele']))
        {
            $where['u.tele'] = array('LIKE','%'.$_GET['tele'].'%');
        }

        //查询用户昵称
        if (!empty($_GET['username']))
        {
            $where['u.username'] = array('LIKE','%'.$_GET['username'].'%');
        }

        //查询开始时间
        if (!empty($_GET['stime']))
        {
            $where['o.add_time'][] = array('EGT',$_GET['stime']);
        }

        //查询结束时间
        if (!empty($_GET['etime']))
        {
            $where['o.add_time'][] = array('ELT',$_GET['etime']);
        }

        //升级状态
        $listLotteryStatus = [
            1=>'未升级',
            '升级中',
            '升级成功',
            10=>'升级失败'
        ];
        $this->assign('listLotteryStatus',$listLotteryStatus);
        if ($_GET['lottery'])
        {
            $where['o.lottery'] = $_GET['lottery']-1;
        }

        //查询订单状态
        $listOrderStatus = [
            1=>'代发货',
            2=>'已发货',
            5=>'已完成',
            7=>'已退款',
        ];
        $this->assign('listOrderStatus',$listOrderStatus);
        if (!empty($_GET['status']))
        {
            $where['o.status'] = $_GET['status'];
        }

        //f分页设置
        $model_order = M('order');
        $page_size = 20;
        $count = $model_order->alias('o')
            ->join('ins_user as u ON u.id=o.uid')
//            ->field('u.username,u.tele,u.tgroup_4,u.tgroup_5,o.prices,o.lottery,o.status,u.tgroup_3,u.tgroup_2,u.tgroup_1,u.topkey')
            ->field('o.total,o.lottery_total,o.lottery,o.status')
            ->where($where)
            ->count();
        $pager = new Page($count,$page_size);
        /*
         *统计 start
         * 别问我 别的程序员没有备注
         * 不是我写的
         * 筛选条件不确定，所以直接引用自本类中的已完成方法
         * 能用 别问我为什么
         * 可以搜索一下查看源代码
         **/
        $stime = date('Y-m-d') . ' 04:00:00';
        $etime = date('Y-m-d') . ' 23:59:59';
        $order_map['u.topkey'] = $topkey;
        $order_map['u.tgroup_'.$tgroup] = $uid;
        $order_map['o.lottery'] = array('IN',array(1,2,9));
        $order_map['o.type'] = 0;
        ($id = $this->_get('id', 'intval',0)) && $order_map['u.id'] = $id;
        ($lottery = $this->_get('lottery', 'intval',0)) && $order_map['o.lottery'] = $lottery;
        ($tele = $this->_get('tele', 'trim','')) && $order_map['u.tele'] = array('like','%'.$tele.'%');
        ($map_stime = $this->_request('stime', 'trim')) && $order_map['o.add_time'][] = array('egt', $map_stime);
        ($map_etime = $this->_request('etime', 'trim')) && $order_map['o.add_time'][] = array('elt', $map_etime);
        ($tgroup_1 = $this->_get('tgroup_1', 'intval',0)) && $order_map['u.tgroup_1'] = $tgroup_1;
        ($tgroup_2 = $this->_get('tgroup_2', 'intval',0)) && $order_map['u.tgroup_2'] = $tgroup_2;
        ($tgroup_3 = $this->_get('tgroup_3', 'intval',0)) && $order_map['u.tgroup_3'] = $tgroup_3;
        ($tgroup_4 = $this->_get('tgroup_4', 'intval',0)) && $order_map['u.tgroup_4'] = $tgroup_4;

        //订单总金额
        $order['total'] = D('order')
            ->alias('o')
            ->join(table('user').' as u ON o.uid = u.id')
            ->where($order_map)
            ->sum('prices');
        $arr=  D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->field('u.id,u.weixin')->where($order_map)->order('u.id desc')->select();
        //升级成功退款金额
        $s_r_map = $s_d_map = $s_w_map = $d_map = $up_map = $order_map;
        $s_r_map['o.status'] = array('IN',[8,9]);
        $s_r_map['o.lottery'] = 2;
        $order['success_refund'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($s_r_map)->sum('lottery_total');
        //升级成功提货金额
        $s_d_map['o.status'] = array('IN',[3,4,5]);
        $s_d_map['o.lottery'] = 2;
        $order['success_delivery'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($s_d_map)->sum('lottery_total');
        //升级成功待处理金额统计
        $s_w_map['o.status'] = 2;
        $s_w_map['o.lottery'] = 2;
        $order['success_wait'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($s_w_map)->sum('lottery_total');
        //升级失败金额
        $d_map['o.lottery'] = 9;
        $order['failed'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($d_map)->sum('prices');
        //升级中金额
        $up_map['o.lottery'] = 1;
        $order['uporder'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($up_map)->sum('prices');

        $order_map['o.add_time'][] =  array('elt', $etime);
        $order_map['o.add_time'][] =  array('egt', $stime);
        $order['days'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($order_map)->sum('prices');

        $order['total'] = $order['total'] ? $order['total'] : 0.00;
        $order['success_refund'] = $order['success_refund'] ? $order['success_refund'] : 0.00;
        $order['success_delivery'] = $order['success_delivery'] ? $order['success_delivery'] : 0.00;
        $order['success_wait'] = $order['success_wait'] ? $order['success_wait'] : 0.00;
        $order['failed'] = $order['failed'] ? $order['failed'] : 0.00;
        $order['uporder'] = $order['uporder'] ? $order['uporder'] : 0.00;
        $order['days'] = $order['days'] ? $order['days'] : 0.00;

        $this->assign('order',$order);
        $arr1=[];
        foreach ($arr as $key => $val) {
            $arr1[$key] = $val[id];
        }
        $arr1 = (!empty($arr)) ? $arr1 : Array(0) ;
        $kid=array_unique($arr1);


        $map_['uid'] = array('IN',$kid);
        $map_['action'] = 'commission';

        ($map_stime = $this->_request('stime', 'trim')) && $map_['add_time'][] = array('egt', $map_stime);
        ($map_etime = $this->_request('etime', 'trim')) && $map_['add_time'][] = array('elt', $map_etime);
        $commission = D('price_log')->where($map_)->sum('price');
        $commission = $commission ? $commission : 0.00;
        $this->assign('commission',$commission);
        $map_['action'] = 'express';
        $express = D('price_log')->where($map_)->sum('price');
        $this->assign('express',$express);
        /**************************************************** 统计 end **************************************************************/

        //查询订单 联合用户表
        $lists = $model_order->alias('o')
            ->join('ins_user as u ON u.id=o.uid')
            ->field('u.username,u.tele,u.tgroup_4,u.tgroup_5,o.prices,o.lottery,o.status,u.tgroup_3,u.tgroup_2,u.tgroup_1,u.topkey')
            ->where($where)
            ->limit($pager->firstRow,$pager->listRows)
            ->select();
        $page = $pager->show();
        $this->assign('page',$page);
        // 导出执行的查询
        if($this->_request('export') != ''){
            $list1 = $model_order->alias('o')
                ->join('ins_user as u ON u.id=o.uid')
                ->field('u.username,u.tele,u.tgroup_4,u.tgroup_5,o.prices,o.lottery,o.status,u.tgroup_3,u.tgroup_2,u.tgroup_1,u.topkey')
                ->where($where)
                ->select();

            //处理所有
            $data = [];
            foreach ($list1 as $k=>$valu)
            {
                $valu['tgroup_4'] = $this->getUserNmae($valu['tgroup_4']);
                $valu['tgroup_5'] = $this->getUserNmae($valu['tgroup_5']);
                $valu['tgroup_3'] = $this->getUserNmae($valu['tgroup_3']);
                $valu['tgroup_2'] = $this->getUserNmae($valu['tgroup_2']);
                $valu['tgroup_1'] = $this->getUserNmae($valu['tgroup_1']);
                $valu['status'] = $listOrderStatus[$valu['status']];
                $valu['lottery'] = $listLotteryStatus[$valu['lottery']];
                $data[$k] = $valu;
            }

            Vendor('excelClass.excelclass');
            $excel = new excelClass();
            $excel->echoOrderInfo('订单明细'.date('YmdHis').'.xls',$data);
            exit;
        }

        //处理所有
        $data = [];
        foreach ($lists as $k=>$value)
        {
            $value['tgroup_4'] = $this->getUserNmae($value['tgroup_4']);
            $value['tgroup_5'] = $this->getUserNmae($value['tgroup_5']);
            $value['tgroup_3'] = $this->getUserNmae($value['tgroup_3']);
            $value['tgroup_2'] = $this->getUserNmae($value['tgroup_2']);
            $value['tgroup_1'] = $this->getUserNmae($value['tgroup_1']);
//            $tmp = $value['tgroup_1'];
            $value['status'] = $listOrderStatus[$value['status']];
            $value['lottery'] = $listLotteryStatus[$value['lottery']];
            $data[$k] = $value;
        }
        //分配查询列表
        $this->assign('lists',$data);
        $this->display();
    }

    /*
* 历史资金报表
*/
    public function history_list()
    {
        //自己的团队
        $where = [];
        //自己的级别
        $selfTgroup = $this->visitor->info['tgroup'];
        //自己的id
        $selfUid = $this->visitor->info['id'];
        //三级联动
        if ($_GET['uid'])
        {
            //获取查询的uid 和级别
            $uid = $_GET['uid'];
            $tmp = M('user')
                ->field('tgroup,topkey')
                ->where(['id'=>$uid])
                ->find();
            $tgroup = $tmp['tgroup'];
            $topkey = $tmp['topkey'];
        }else{
            //获取查询自己的uid 和级别
            $uid = $selfUid;
            $tgroup = $selfTgroup;
            $topkey = $this->visitor->get('topkey');
        }

        //显示团队名
        $tmp_add_arr = [0=>'普通用户',1=>'总经销商',2=>'经销商',3=>'代理商',4=>'经纪人',5=>'全民经纪人'];
        $this->assign('tmp_add_arr',$tmp_add_arr);
        $this->assign('tgroup',$tgroup);
        $tuanNmae = $tmp_add_arr[$tgroup];
        $this->assign('tuanNmae',$tuanNmae);

        //查询团队列表
        $listTeams = getTeamList($selfTgroup,$selfUid);
        $this->assign('listTeams',$listTeams);

        //判断是否最后一个
        $tmp_whe["tgroup_".$tgroup] = $uid;
        $info_where = [];
        $tmp_if = M('user')
            ->field('id')
            ->where($tmp_whe)
            ->find();
        //获取用户昵称
        $new_username = $this->getUserNmae($uid);
        $this->assign('uid',$uid);
        $this->assign('new_username',$new_username);
        if (empty($tmp_if))
        {
            $where["uid"] = $uid;
        }else{
            $where["tgroup_".$tgroup] = $uid;
        }

        //查询用户账号
        if (!empty($_GET['tele']))
        {
            $where['tele'] = array('LIKE','%'.$_GET['tele'].'%');
        }

        //查询用户昵称
        if (!empty($_GET['username']))
        {
            $where['username'] = array('LIKE','%'.$_GET['username'].'%');
        }

        //查询开始时间
        if (!empty($_GET['stime']))
        {
            $where['ktime'][] = array('EGT',$_GET['stime']);
        }

        //查询结束时间
        if (!empty($_GET['etime']))
        {
            $where['ktime'][] = array('ELT',$_GET['etime']);
        }

        //级别
        if ($_GET['tgroup'] != 'all' && !empty($_GET['tgroup']))
        {
            $where['tgroup'] = $_GET['tgroup'];
        }

        //f分页设置
        $model_order = M('commission_summary');
        $page_size = 20;
        $count = $model_order->where($where)->count();
        $pager = new Page($count,$page_size);

        //查询订单 联合用户表
        $lists = $model_order->where($where)
            ->limit($pager->firstRow,$pager->listRows)
            ->select();
        $page = $pager->show();
        $this->assign('page',$page);
        // 导出执行的查询
        if($this->_request('export') != ''){
            $list1 = $model_order->where($where)->select();

            //处理所有
            $data = [];
            foreach ($list1 as $k=>$valu)
            {
                $valu['tgroup_3'] = $this->getUserNmae($valu['tgroup_3']);
                $valu['tgroup_2'] = $this->getUserNmae($valu['tgroup_2']);
                $valu['tgroup_1'] = $this->getUserNmae($valu['tgroup_1']);
                $valu['tgroup'] = $tmp_add_arr[$valu['tgroup']];
                $data[$k] = $valu;
            }

            Vendor('excelClass.excelclass');
            $excel = new excelClass();
            $excel->echoMoneyList('历史资金列表'.date('YmdHis').'.xls',$data);
            exit;
        }

        //处理所有
        $data = [];
        foreach ($lists as $k=>$value)
        {
            $value['tgroup_3'] = $this->getUserNmae($value['tgroup_3']);
            $value['tgroup_2'] = $this->getUserNmae($value['tgroup_2']);
            $value['tgroup_1'] = $this->getUserNmae($value['tgroup_1']);
            $value['tgroup'] = $tmp_add_arr[$value['tgroup']];
            $data[$k] = $value;
        }
        var_dump($model_order->getLastSql());
        //分配查询列表
        $this->assign('lists',$data);
        $this->display();
    }

    //ajax获取联动
    //
    public function getNextUids()
    {
        //获取查询的uid 和级别

        $uid = $_GET['uid'];
        $tmp = M('user')->field('tgroup')->where(['id'=>$uid])->find();

        //如果有
        $tgroup = $tmp['tgroup'];
        $listTeams = getTeamList($tgroup,$uid);
        //处理成字符串
        $str = '<span class="standard_select fl">
<span class="select_shelter">
<select id="" name="uid" onchange="getNestUids(this)" class="order_select fl" style="width:112px;">';

        //判断是否最后
        if (empty($listTeams))
        {
            echo json_encode('');
        }else{
            $str .= '<option value="'.$uid.'">请选择</option>';
            foreach ($listTeams as $k=>$v)
            {
                $str .= '<option value="'.$v['id'].'">'.$v['username'].'</option>';
            }
            $str .= '</select></span></span>';
            echo json_encode($str);
        }

    }
//    获取用户名
    public function getUserNmae($id)
    {
        static $arr = [];
        if (isset($arr[$id]))
        {
            return $arr[$id];
        }else{
            $tmp = M('user')->field('username')->where(['id'=>$id])->find();
            $arr[$id] = $tmp['username'];
        }
        return $arr[$id];
    }


    /*资金流水明细*/
    public function price_list()
    {
        //自己的团队
        $where = [];
        //自己的级别
        $selfTgroup = $this->visitor->info['tgroup'];
        //自己的id
        $selfUid = $this->visitor->info['id'];
        //三级联动
        //[0=>'普通用户',1=>'总经销商',2=>'经销商',3=>'代理商',4=>'经纪人',5=>'全民经纪人'];
        if ($_GET['uid'])
        {
            //获取查询的uid 和级别
            $uid = $_GET['uid'];
            $tmp = M('user')->field('tgroup')->where(['id'=>$uid])->find();
            $tgroup = $tmp['tgroup'];
        }else{
            //获取查询自己的uid 和级别
            $uid = $selfUid;
            $tgroup = $selfTgroup;
        }
        //显示团队名
        $tmp_add_arr = [0=>'用户名',1=>'总经销商',2=>'经销商',3=>'代理商',4=>'经纪人',5=>'全民经纪人'];
        $tuanNmae = $tmp_add_arr[$tgroup];
        $this->assign('tuanNmae',$tuanNmae);

        //获取用户昵称
        $new_username = $this->getUserNmae($uid);
        $this->assign('uid',$uid);
        $this->assign('new_username',$new_username);
        //判断是否最后一个
        $tmp_whe["tgroup_".$tgroup] = $uid;
        $tmp_if = M('user')
            ->field('id')
            ->where($tmp_whe)
            ->find();
        if (empty($tmp_if))
        {
            $where["u.id"] = $uid;
        }else{
            $where["u.tgroup_".$tgroup] = $uid;
        }
        //查询团队列表
        $listTeams = getTeamList($selfTgroup,$selfUid);
        $this->assign('listTeams',$listTeams);



        if (!empty($_GET['username']))
        {
            $where['u.uname'] = array('LIKE','%'.$_GET['username'].'%');
        }
        if (!empty($_GET['tele']))
        {
            $where['u.tele'] = array('LIKE','%'.$_GET['tele'].'%');
        }
        if (!empty($_GET['tgroup']))
        {
            $tgroup = ($_GET['tgroup'] != 100) ? $_GET['tgroup'] : 0;
            $where['u.tgroup'] = $tgroup;
        }
        if (!empty($_GET['topkey']))
        {
            $where['u.topkey'] = array('LIKE','%'.$_GET['topkey'].'%');
        }

        if (!empty($_GET['action'])) {
            $action = $_GET['action'];
            if ($_GET['action'] != -1) {
                $where['action'] = $_GET['action'];
            }
            $this->assign('action_', $action);
        }

        if (!empty($_GET['stime'])) {
            $time_start = $_GET['stime'];
            $time_end = $_GET['etime'] ;
            $where['add_time'] = array(array('egt', $time_start), array('elt', $time_end));
        }
        //分页实现
        $count = D('price_log_ext')
            ->alias('u')
            ->where($where)
            ->count();
        $page_size = 20;
        $pager = new Page($count, $page_size);
        $this->assign('page',$pager);

        $tmp_tmp = $where;
        //$list
        $list = D('price_log_ext')
            ->alias('u')
            ->where($where)
            ->order('add_time desc')
            ->limit($pager->firstRow.','.$pager->listRows)
            ->select();
        //处理类型
        $tmp_action = [
            'order'=>'消费',
            'cash'=>'现金',
            'recharge'=>'充值',
            'order_refund'=>'退款',
            'commission'=>'返佣',
            'express'=>'运费'
        ];
        //处理所有
        foreach ($list as $key => $val) {
            $list[$key]['get_tgroup']=get_tgroup($_GET['tgroup']);
            $tmp = $val['action'];
            $list[$key]['action1']=$tmp_action[$tmp];
            $list[$key]['tgroup_1_name'] = D('user')->where(['id' => $val['tgroup_1']])->getField('username');
            $list[$key]['tgroup_2_name'] = D('user')->where(['id' => $val['tgroup_2']])->getField('username');
            $list[$key]['tgroup_3_name'] = D('user')->where(['id' => $val['tgroup_3']])->getField('username');
            $list[$key]['tgroup_4_name'] = D('user')->where(['id' => $val['tgroup_4']])->getField('username');
            $list[$key]['tgroup_5_name'] = D('user')->where(['id' => $val['tgroup_5']])->getField('username');
        }
        $page = $pager->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        // 引入excel类文件，将订单用excel文件导出来
        if ($this->_request('export') != '') {
            $list_tt = D('price_log_ext')
                ->alias('u')
                ->where($tmp_tmp)
                ->order('add_time desc')
                ->select();
            if (empty($tmp_if))
            {
                $tmp_tmp["u.id"] = $uid;
            }else{
                $tmp_tmp["u.tgroup_".$tgroup] = $uid;
            }
            //var_dump($tmp_tmp);die;
            //处理所有
            foreach ($list_tt as $key => $val) {
                $list_tt[$key]['get_tgroup']=get_tgroup($_GET['tgroup']);
                $tmp = $val['action'];
                $list_tt[$key]['action1']=$tmp_action[$tmp];
                $list_tt[$key]['tgroup_1_name'] = D('user')->where(['id' => $val['tgroup_1']])->getField('username');
                $list_tt[$key]['tgroup_2_name'] = D('user')->where(['id' => $val['tgroup_2']])->getField('username');
                $list_tt[$key]['tgroup_3_name'] = D('user')->where(['id' => $val['tgroup_3']])->getField('username');
                $list_tt[$key]['tgroup_4_name'] = D('user')->where(['id' => $val['tgroup_4']])->getField('username');
                $list_tt[$key]['tgroup_5_name'] = D('user')->where(['id' => $val['tgroup_5']])->getField('username');
            }
            //**********************
            Vendor('excelClass.excelclass');
            $excel = new excelClass();
            $excel->echoprice_listFile('资金流水明细表' . date('YmdHis') . '.xls', $list_tt);
            exit;
        }

        //记录信息
        $this->assign('search', array(
            'keyword' =>$_GET['username'] ,
            'realname' => $_GET['realname'],
            'remark' => 123,
            'tgroup' => $_GET['tgroup'],
            'topkey' => $_GET['topkey'],
            'type' => $type[1],
            'stime' => $_GET['stime'],
            'etime' => $_GET['etime'],
        ));

        $this->display();
    }
	/* 创建成员 */
	public function add_user(){
		if(IS_POST){
			$data = D('user')->create();
			for($i=1;$i<$data['tgroup'];$i++){
				if($data['tgroup_'.$i] == ''){
					$this->ajaxReturn(0,'请选择'.$this->tgroup[$i].'！');
				}
			}
			if($data['tgroup'] == ''){
				$this->ajaxReturn(0,'团队级别不能为空！');
			}
			if($data['username'] == ''){
				$this->ajaxReturn(0,'用户名不能为空！');
			}
			if($data['tele'] == ''){
				$this->ajaxReturn(0,'帐号电话不能为空！');
			}
			$Api = A('app://Api');

			$is_mobile = $Api->is_mobile($data['tele']);

			if($is_mobile == 0){
			$this->ajaxReturn(0,'手机号不正确！');
			}
//			if(ceil($data['pers']) != $data['pers']){
//				$this->ajaxReturn(0,'收益比例不能为小数');
//			}
			if($data['tgroup'] > 1){
				$tgroup_top_user_id = $data['tgroup_'.($data['tgroup']-1)];
				$tgroup_top_user = D('user')->field('pers')->find($tgroup_top_user_id);
				if($data['pers'] > $tgroup_top_user['pers']){
					$this->ajaxReturn(0,'收益比例不能大于'.$tgroup_top_user['pers'].'%');
				}
			}
			if ($data['password'] != '' && $_POST['rpassword'] != '') {
				if (trim($data['password']) != trim($_POST['rpassword'])) {
					$this->ajaxReturn(0,'两次输入的帐号密码不一致，请重新填写！');
				}
			} else {
				$this->ajaxReturn(0,'帐号密码不能为空，请重新填写！');
			}
			if (!$data['reg_time']) {
				$data['reg_time'] = date('Y-m-d H:i:s', time());
			}
			if (!$data['status']) {
				$data['status'] = 1;
			}
			if ($data['tele']) {
				//手机号码是否重复
				if (D('user')->where(array('tele' => $data['tele']))->count()) {
					$this->ajaxReturn(0,'手机号码已经存在，请重新填写！');
				}
			}
			if ($data['username']) {
				//会员昵称是否重复
				if (D('user')->where(array('username' => $data['username']))->count()) {
					$this->ajaxReturn(0,'用户名已经存在，请重新填写！');
				}
			}
			$data['password'] = md5($data['password']);
			$data['topkey'] = $this->visitor->get('topkey');
			//$data['created_id'] = $this->visitor->info['id'];
			$data['invite_uid'] = $this->visitor->info['id'];
			D('user')->add($data);
			$this->ajaxReturn(1,'帐号添加成功','','add_user');
		}else{
			if($this->visitor->get('tgroup') == 1){
				$level = [3,4];
			}elseif($this->visitor->get('tgroup') == 2){
				$level = [3,4];
			}elseif($this->visitor->get('tgroup') == 3){
				$level = [4];
			}else{
				$this->redirect('user/index');
			}
			$this->assign('level', $level);
			$this->assign('tgroupid', $this->visitor->get('tgroup'));
			  $username = $this->visitor->get('username');
	                        $id = $this->visitor->get('id');
	                        $topkey = $this->visitor->get('topkey');
	                        $this->assign('username',$username);
	                         $this->assign('id',$id);
	                          $this->assign('topkey',$topkey);

			$this->display();
		}
	}

	/* 二维码 */
	public function erweima(){
        $code_url = U('mall/passport/binding_invite',array('invite_uid'=> $this->visitor->info['id']),true,false,true);
		$this->assign('code_url', $code_url);
		$this->assign('code', $this->visitor->info['id']);
		$count = D('user')->where(array('invite_uid'=>$this->visitor->info['id']))->count();
		$this->assign('count', $count);
		$this->assign('uname', $this->visitor->info['username']);
		
		$invite_uid = $this->visitor->get('invite_uid');
		$recommend_user = D('user')->find($invite_uid);

		$this->display();
	}

	public function erweima_img()
    {
        $code = $this->_request('code', 'trim', '');
        Vendor('qrcode.qrcode');
        $fqrcode = new fqrcode();
        $data = $fqrcode->getCode($code, 'code');
        exit;
    }

	/* 用户联动 */
	public function tgroup_child(){
		$id = $this->_get('id','intval',0);
		$topkey = $this->visitor->get('topkey');
		$this_user_tgroup = $this->visitor->get('tgroup');
		$this_user_tgroup_1 = $this->visitor->get('tgroup_1');
		$this_user_tgroup_2 = $this->visitor->get('tgroup_2');
		$this_user_tgroup_3 = $this->visitor->get('tgroup_3');
		$this_user_tgroup_4 = $this->visitor->get('tgroup_4');
		if($id == 0){
			$map['id'] = $this_user_tgroup == 1 ? $this->visitor->info['id'] : $this_user_tgroup_1;
			$next_tgroup = 1;
		}else{
			$choice_user = D('user')->field('tgroup')->find($id);
			$next_tgroup = $choice_user['tgroup'] + 1;
			$map['tgroup'] = $next_tgroup;
			$this_next_tgroup = 'this_user_tgroup_'.$next_tgroup;
			if($$this_next_tgroup > 0){//我的上级
				$map['id'] = $$this_next_tgroup;
			}elseif($next_tgroup == $this_user_tgroup){//我的
				$map['id'] = $this->visitor->info['id'];
			}elseif($next_tgroup > $this_user_tgroup && $this_user_tgroup > 0){//我的下级
				$map['tgroup_'.$this_user_tgroup] = $this->visitor->info['id'];
			}

			//所选下级
			$map['tgroup_'.$choice_user['tgroup']] = $id;

		}

		$map['topkey'] = $topkey;
		$list = D('user')->where($map)->field('id,username as name')->select();
		if(!$list){
			$this->ajaxReturn(0,'该用户下没有'.$this->tgroup[$next_tgroup].',请先添加');
		}else{
			$this->ajaxReturn(1,'',array('list'=>$list,'top_option'=>'请选择'.$this->tgroup[$next_tgroup]));
		}
	}

	public function get_tgroup_data(){
		$select_tgroup = $this->_request('select_tgroup','intval',0);
		$tgroup = $this->visitor->get('tgroup');

		for($i=1;$i<$tgroup;$i++){
			$data['tgroup_'.$i] = $this->visitor->get('tgroup_'.$i);
		}
		$data[] = $this->visitor->info['id'];
		$data[] = '';
		$this->assign('select_nums', ($select_tgroup - 1));
		$this->assign('data', $data);
		$response = $this->fetch();
		$this->ajaxReturn(1, '', $response);
	}

	/* 消费树图 */
	public function order_total(){
		$id = $this->_get('id','intval');
		$user = D('user')->field('id,username,tele,img,orders,status')->find($id);
		$order_map['o.lottery'] = array('IN',array(1,2,9));
		$order_map['o.type'] = 0;
		$order_map['u.id'] = $user['id'];
		$order_total_list = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($order_map)->field('o.prices as prices,o.lottery_total as lottery_total,o.lottery as lottery')->select();
		$order_total = 0.00;
		foreach($order_total_list as $tval){
			$order_total += ($tval['lottery'] == 2 ? $tval['lottery_total'] : $tval['prices']);
		}
		$user['order_total'] = $order_total ? $order_total : 0.00;
		$this->assign("user", $user);
		$this->display();
	}

	/* 获取下级 */
	public function get_child(){
		$id = $this->_request('id','intval');
		$userinfo = D('user')->field('tgroup,id,topkey')->find($id);
		$this_user_tgroup = $userinfo['tgroup'];
		if($this_user_tgroup < 1){
			$this->ajaxReturn(0, '');
		}
		$map['tgroup_'.$this_user_tgroup] = $userinfo['id'];
		if($this_user_tgroup < 3){
			$map['tgroup'] = $this_user_tgroup+1;
		}
		$map['topkey'] = $userinfo['topkey'];
		$count = D('user')->where($map)->count();
		$page_size = 30;
		$pager = new Page($count, $page_size);
		$list = D('user')->field('id,username,tele,img,orders,reg_time,last_time,status,tgroup')->where($map)->limit($pager->firstRow . ',' . $pager->listRows)->select();
		foreach($list as $key=>$val){
			$list[$key]['nums'] = D('user')->where(array('tgroup_'.$val['tgroup']=>$val['id']))->count();
			$list[$key]['img'] = avatar($val['img'], 64);

			$order_map['o.lottery'] = array('IN',array(1,2,9));
			$order_map['o.type'] = 0;
			$order_map['u.id'] = $val['id'];

			$order_total = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($order_map)->sum('prices');
			$list[$key]['order_total'] = $order_total ? $order_total : 0.00;
		}
		$this->ajaxReturn(1, '', $list);
	}
	
	/* 资金树图 */
	public function price_total(){
		$id = $this->_get('id','intval');
		$user = D('user')->field('id,username,tele,img,orders,status,topkey')->find($id);

		$recharge_map['u.topkey'] = $user['topkey'];
		$recharge_map['u.id'] = $user['id'];
		$recharge_map['ur.status'] = 1;

		//充值总额
		$recharge_map['ur.type'] = 1;
		$recharge_total = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');
		$user['recharge_total'] = $recharge_total ? $recharge_total : 0.00;

		//提现总额
		$recharge_map['ur.type'] = 2;
		$cash_total = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');
		$user['cash_total'] = $cash_total ? $cash_total : 0.00;

		$this->assign("user", $user);
		$this->display();
	}

	/* 获取下级 */
	public function get_price_child(){
		$id = $this->_request('id','intval');
		$userinfo = D('user')->field('tgroup,id,topkey')->find($id);
		$this_user_tgroup = $userinfo['tgroup'];
		if($this_user_tgroup < 1){
			$this->ajaxReturn(0, '');
		}
		$map['tgroup_'.$this_user_tgroup] = $userinfo['id'];
		if($this_user_tgroup < 3){
			$map['tgroup'] = $this_user_tgroup+1;
		}
		$map['topkey'] = $userinfo['topkey'];
		$count = D('user')->where($map)->count();
		$page_size = 30;
		$pager = new Page($count, $page_size);
		$list = D('user')->field('id,username,tele,img,orders,reg_time,last_time,status,tgroup')->where($map)->limit($pager->firstRow . ',' . $pager->listRows)->select();
		foreach($list as $key=>$val){
			$list[$key]['nums'] = D('user')->where(array('tgroup_'.$val['tgroup']=>$val['id']))->count();
			$list[$key]['img'] = avatar($val['img'], 64);

			$recharge_map['u.id'] = $val['id'];
			$recharge_map['ur.status'] = 1;

			//充值总额
			$recharge_map['ur.type'] = 1;
			$recharge_total = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');
			$recharge_total = $recharge_total ? $recharge_total : 0.00;

			//提现总额
			$recharge_map['ur.type'] = 2;
			$cash_total = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');
			$cash_total = $cash_total ? $cash_total : 0.00;

			$list[$key]['recharge_total'] = $recharge_total ? $recharge_total : 0.00;
			$list[$key]['cash_total'] = $cash_total ? $cash_total : 0.00;

		}
		$this->ajaxReturn(1, '', $list);
	}

	/* 个人资料 */
    public function profile()
    {
        if (IS_POST) {
            $year          = $this->_post('year', 'int');
            $month         = $this->_post('month', 'int');
            $day           = $this->_post('day', 'int');
            $data['birthday']      = $year . '-' . $month . '-' . $day;
            $data['sex']   = $this->_post('sex', 'intval');
            $data['email'] = $this->_post('email', 'trim');
            $data['weixin']   = $this->_post('weixin', 'trim');
            $data['username']   = $this->_post('username', 'trim');
            if ($img = $this->_post('img', 'trim')) {
                $data['img'] = $img;
            }
            D('user')->where(['id' => $this->get_visitor_id()])->save($data);
            
            $this->ajaxResultSuccess('修改成功', ['url' => $this->_post('ret_url')]);
        }
        $info = D('user')->get($this->get_visitor_id(), 'id,username,sex,email,weixin,img,tele,pers');
        //D('yi_order')->where(['uid'=>$info['id']])->setField('uname', $info['username']);
        
        if (strpos($info['email'], "@default.com")) {
            $info['email'] = '';
        }
        $info['birthday'] = D('user')->where(array('id' => $this->get_visitor_id()))->getField('birthday');
        $explode          = explode('-', $info['birthday']);
        $info['year']     = $explode[0] ? $explode[0] : '1980';
        $info['month']    = $explode[1] ? $explode[1] : '01';
        $info['day']      = $explode[2] ? $explode[2] : '01';
        $this->assign('ret_url', $_SERVER["HTTP_REFERER"]);
        
        $this->assign('info', $info);
        $this->display();
    }

	/* 修改密码 */
    public function password()
    {
        if (IS_POST) {
            $oldpassword = $this->_post('oldpassword', 'trim');
            $password    = $this->_post('password', 'trim');
            $repassword  = $this->_post('repassword', 'trim');
            if (!$password) {
                $this->ajaxResultError('请输入新密码');
            }
            if ($password != $repassword) {
                $this->ajaxResultError('两次输入的密码不一致');
            }

            $passlen = strlen($password);
            if ($passlen < 6 || $passlen > 20) {
                $this->ajaxResultError("密码长度在6~20个之间!");
            }
            //连接用户中心
            $passport = $this->_user_server();
            $result   = $passport->edit($this->uid, $oldpassword, array('password' => $password));
            D('user')->where(['id' => $this->get_visitor_id()])->save(['salt' => '0']);

            if ($result) {
                $this->ajaxResultSuccess();
            }
            else {
                $this->ajaxResultError($passport->get_error());
            }
        }
        $this->_config_seo();
        $this->display();
    }


    public function pers(){
		$topkey = $this->visitor->get('topkey');
		$where['topkey'] = $topkey;
		$where['tgroup_'.$this->user_tgroup] = $this->uid;

		if($tgroup = $this->_get('tgroup', 'intval',0)){
			$where['tgroup'] = $tgroup;
		}else{
			$where['tgroup'] = ['gt',0];
		}
		($tele = $this->_get('tele', 'trim','')) && $where['tele'] = array('like',$tele);


		$page_size = 20;
		$count = D('user')->where($where)->count();
		$pager = new Page($count,$page_size);
		$list = D('user')->where($where)->field('pers,tgroup,username,id')->limit($pager->firstRow, $pager->listRows)->order('reg_time desc')->select();


		$page = $pager->show();
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search',array('tgroup'=>$tgroup,'tele'=>$tele));
        $this->display();
	}

    public function set_pers(){
		$id = $this->_post('id', 'intval');
		$pers = $this->_post('pers', 'trim');
		if(ceil($pers)!=$pers){
			$this->ajaxResultSuccess('收益比例不能为小数',array('error'=>1));
		}
		$user_info = D('user')->field('tgroup,tgroup_1,tgroup_2,tgroup_3,tgroup_4')->find($id);
		if($user_info['tgroup'] > 1){
			$tgroup_top_user_id = $user_info['tgroup_'.($user_info['tgroup']-1)];
			$tgroup_top_user = D('user')->field('pers')->find($tgroup_top_user_id);
			if($pers > $tgroup_top_user['pers']){
				$this->ajaxResultSuccess('收益比例不能大于上级的收益比例<br>该帐号上级收益比例为'.$tgroup_top_user['pers'].'%',array('error'=>1));
			}
		}
		D('user')->where(['id'=>$id,'tgroup_'.$this->user_tgroup=>$this->uid])->save(array('pers'=>$pers));
		$this->ajaxResultSuccess('');
	}

	/**
	 * 获取某月月头/月底时间戳
	 * $m 本月0,上月-1,上2月-2
	 * $t false月头,true月底
	 */

    public function price_log(){
		
		$s_date = $this->_get('month', 'trim', '');
		if($s_date == ''){
			$d = date('d');
			if($d<15){
				$s_date = date('Ym',strtotime('-1 month')).'1';
			}else{
				$s_date = date('Ym',time()).'0';
				$month[] = array('date'=>$s_date,'name'=>date('Y').'年'.date('m').'月上旬');
			}
		}
		for($i=1;$i<=12;$i++){
			$y = date('Y',strtotime('-'.$i.' month'));
			$m = date('m',strtotime('-'.$i.' month'));
			$date = date('Ym',strtotime('-'.$i.' month'));
			$month[] = array('date'=>$date.'1','name'=>$y.'年'.$m.'月下旬');
			$month[] = array('date'=>$date.'0','name'=>$y.'年'.$m.'月上旬');
		}
		$this->assign('month',$month);

		$map_year = substr($s_date,0,4);
		$map_month = substr($s_date,4,2);
		$map_type = substr($s_date,6,1);
		if($map_type == 1){
			$end_time =$this->getMonth($map_year,$map_month,true);
			$start_time = date('Y-m-d H:i:s',strtotime('- 15 days',strtotime($end_time))-86399);
		}else{
			$start_time = $this->getMonth($map_year,$map_month);
			$end_time = date('Y-m-d H:i:s',strtotime('+ 15 days',strtotime($start_time)));
		}

		$topkey = $this->visitor->get('topkey');

		$map['u.topkey'] = $topkey;
		$map['u.tgroup_'.$this->user_tgroup] = $this->uid;
		$map['o.lottery'] = array('IN',array(1,2,9));
		$map['o.type'] = 0;
		$map['o.add_time'][] = array('egt',$start_time);
		$map['o.add_time'][] = array('elt',$end_time);

		($tgroup_1 = $this->_get('tgroup_1', 'intval',0)) && $map['u.tgroup_1'] = $tgroup_1;
		($tgroup_2 = $this->_get('tgroup_2', 'intval',0)) && $map['u.tgroup_2'] = $tgroup_2;
		($tgroup_3 = $this->_get('tgroup_3', 'intval',0)) && $map['u.tgroup_3'] = $tgroup_3;
		($tgroup_4 = $this->_get('tgroup_4', 'intval',0)) && $map['u.tgroup_4'] = $tgroup_4;

		//我的收益比例
		$this_user_pers = $this->visitor->get('pers');

		$page_size = 20;
		$count = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($map)->count();
		$pager = new Page($count,$page_size);
		$list = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->limit($pager->firstRow, $pager->listRows)->where($map)->field('o.prices as prices,o.status as status,o.add_time as add_time,o.lottery as lottery,o.lottery_total as lottery_total,u.id as uid,u.username as username,u.tele as tele,o.id as oid,o.orderid as orderid,u.tgroup_1 as tgroup_1,u.tgroup_2 as tgroup_2,u.tgroup_3 as tgroup_3,tgroup_4 as tgroup_4,u.invite_uid as invite_uid')->order('o.add_time desc')->select();
		$lottery_arr = array(0=>'待升级',1=>'升级中',2=>'升级成功',9=>'升级失败');
		foreach($list as $key=>$val){
			if(in_array($val['status'],[3,4,5,6])){
				$list[$key]['status'] = '已提货';
			}elseif(in_array($val['status'],[9])){
				$list[$key]['status'] = '退款';
			}else{
				//$list[$key]['status'] = '待提货';
				$list[$key]['status'] = '已提货';
			}
			$list[$key]['lottery'] = $lottery_arr[$val['lottery']];
			$list[$key]['tgroup_1_name'] = D('user')->where(['id'=>$val['tgroup_1']])->getField('username');
			$list[$key]['tgroup_2_name'] = D('user')->where(['id'=>$val['tgroup_2']])->getField('username');
			$list[$key]['tgroup_3_name'] = D('user')->where(['id'=>$val['tgroup_3']])->getField('username');
			$list[$key]['tgroup_4_name'] = D('user')->where(['id'=>$val['tgroup_4']])->getField('username');
			$list[$key]['tgroup_5_name'] = D('user')->where(['id'=>$val['tgroup_5']])->getField('username');
			//$pers = D('user')->where(array('id'=>$val['invite_uid']))->getField('pers');
			$prices = $val['lottery'] == 2 ? $val['lottery_total'] : $val['prices'];
			$pers_price = $prices*($this_user_pers*0.01);
			$list[$key]['pers_price'] = $pers_price;
			//$list[$key]['invite_uname'] = D('user')->where(['id'=>$val['invite_uid']])->getField('username');
		}
		$page = $pager->show();

		//收益总额
		$total_prices = 0;
		$total_prices_list = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($map)->select();
		foreach($total_prices_list as $tval){
			$total_prices += ($tval['lottery'] == 2 ? $tval['lottery_total'] : $tval['prices']);
		}
		$total = $total_prices*($this_user_pers*0.01);
		$this->assign(compact('list','total','page'));
		$this->assign('search',array('month'=>$s_date));


        $this->display();
	}

	public function getMonth($y,$m,$t=false){
		$year = $y;
		$month = $m;
		$day = $t ? $this->dayInMonth($month,$year) : 1;
		$hour = $t ? 23 : 0;
		$minute = $t ? 59 : 0;
		$second = $t ? 59 : 0;
		return date('Y-m-d H:i:s',mktime($hour,$minute,$second,$month,$day,$year));
	}

	public function dayInMonth($month,$year){
		return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
	}

	public function del(){
		$id=$_GET[id];
		$map[id]=$id;
		D('user')->where($map)->delete();
		header('Location: http://lv.v-town.cc/team/user/team');

	}

	public function dr($param="") {

		$this->redirect(U('user/team') , array(), 2, ' 删除成功!,2秒钟后页面跳转中 ~');

	}

	function add_teamer() {
		$id=$_GET[id];
		$tgroup   = D('user')->where(array('id' => $id))->getField('tgroup');
		$map['tgroup_'.$tgroup]=$id;

		if ($_GET['username']) {
		$realname= trim($_GET['username']);
		$map['username'] = array('like', '%' . $_GET['username'] . '%');
		$this->assign('username_',$username);

		}

		if ($_GET['tele']) {
		$tele= trim($_GET['tele']);
		$map['tele'] =$tele;
		$this->assign('tele_',$tele);

		}

		if ($_GET[tgroup]) {
		$map['tgroup']= ( $_GET[tgroup]==100) ?0 : $_GET[tgroup] ; 
		$this->assign('tgroup_',$_GET[tgroup]);


		}

		$this->assign('search', array(
		'id' =>$id,
		'type' => $type,
		'tgroup'   => $_GET['tgroup'],
		'tele'   => $_GET['tele'],
		'username'   => $_GET['username'],
		));

		$mod= D('user');
		if (!empty($mod)) {

		if($tgroup>0){
		$result = $this->_list($mod, $map);
		$this->assign('page', $result['page']);
		$list = $result['list'];
		$sum_price = 0;
		foreach($list as $key=>$val){
			$sum_price += $list[$key]['price'];

		}
		$this->assign('sum_price', $sum_price);
		$list1=$mod->where($map)->select();
		foreach($list1 as $key=>$val){
		if(in_array($val['status'],[1])){
		$list1[$key]['status'] = '启用';
		}
		else{
		$list1[$key]['status'] = '无效';
		}
		$list1[$key]['tgroup']=get_tgroup($val[tgroup]);
		$list1[$key]['tgroup_1_name'] = D('user')->where(['id'=>$val['tgroup_1']])->getField('username');
		$list1[$key]['tgroup_2_name'] = D('user')->where(['id'=>$val['tgroup_2']])->getField('username');
		$list1[$key]['tgroup_3_name'] = D('user')->where(['id'=>$val['tgroup_3']])->getField('username');
		$list1[$key]['tgroup_4_name'] = D('user')->where(['id'=>$val['tgroup_4']])->getField('username');
		$list1[$key]['tgroup_5_name'] = D('user')->where(['id'=>$val['tgroup_5']])->getField('username');

		}
		if($this->_request('export') != ''){
		// 引入excel类文件，将订单用excel文件导出来
		Vendor('excelClass.excelclass');
		$excel = new excelClass();
		$excel->echoTeamDetail('团队明细'.date('YmdHis').'.xls',$list1);
		exit;
		}
		$this->assign('list', $list);
		$list1 = $mod->where($map)->field('id')->select();  
		$arr = $this->i_array_column($list1,'id');  
		$arr = (!empty($arr)) ? $arr : Array(0) ;

		$arr= serialize($arr);

		$this->assign('arr', $arr);
		$this->assign('id', $id);

		$this->display();
		}
		else{
		$this->display();
		}

		}


	}

	    public function order_total_ext(){
            $id=$_GET[id];
            $tgroup   = D('user')->where(array('id' => $id))->getField('tgroup');
            $map['tgroup_'.$tgroup]=$id;
            $mod= D('user');
            if($tgroup>=0){
            
              if($tgroup==0){
                   $map_[id]=$id;
                     $list1 = $mod->where($map_)->field('id')->select(); 
                   
               }else{
                    $list1 = $mod->where($map)->field('id')->select();  
               }
           
            $arr = $this->i_array_column($list1,'id');  
            $arr = (!empty($arr)) ? $arr : Array(0) ;
            array_push ( $arr , $_GET[id]);
            
            $map_['id'] = array('IN',$arr);
             $user = D('user')->field('id,username,tele,img,orders,status')->where($map_)->select();
             
                foreach ($user as $k=>$var){
                $order_map['o.lottery'] = array('IN',array(1,2,9));
		$order_map['o.type'] = 0;
		$order_map['u.id'] = $var[id];
		$order_total = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($order_map)->sum('prices');
		$user[$k]['order_total'] = $order_total ? $order_total : 0.00;
         }
           
              $this->assign('list', $user);
              
            $this->display();
            }
            else{
                $this->display();
            }


                }
                
        public function price_total_ext(){
            $id=$_GET[id];
            $tgroup   = D('user')->where(array('id' => $id))->getField('tgroup');
            $map['tgroup_'.$tgroup]=$id;
            $mod= D('user');
        
            if($tgroup>=0){
            if($tgroup==0){
                   $map_[id]=$id;
                     $list1 = $mod->where($map_)->field('id')->select(); 
                   
               }else{
                    $list1 = $mod->where($map)->field('id')->select();  
               } 
            $arr = $this->i_array_column($list1,'id');  
            $arr = (!empty($arr)) ? $arr : Array(0) ;
            array_push ( $arr , $_GET[id]);
            
            $map_['id'] = array('IN',$arr);
             $user = D('user')->field('id,username,tele,img,orders,status')->where($map_)->select();
             
                foreach ($user as $k=>$var){
                $recharge_map['u.id'] = $var['id'];
		$recharge_map['ur.status'] = 1;

		//充值总额
		$recharge_map['ur.type'] = 1;
		$recharge_total = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');
		$user[$k]['recharge_total'] = $recharge_total ? $recharge_total : 0.00;

		//提现总额
		$recharge_map['ur.type'] = 2;
		$cash_total = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');
		$user[$k]['cash_total'] = $cash_total ? $cash_total : 0.00;
         }
           
              $this->assign('list', $user);
              
            $this->display();
            }
            else{
                $this->display();
            }


                }
                
    function i_array_column($input, $columnKey, $indexKey=null){  
        if(!function_exists('array_column')){   
            $columnKeyIsNumber  = (is_numeric($columnKey))?true:false;   
            $indexKeyIsNull            = (is_null($indexKey))?true :false;   
            $indexKeyIsNumber     = (is_numeric($indexKey))?true:false;   
            $result                         = array();   
            foreach((array)$input as $key=>$row){   
                if($columnKeyIsNumber){   
                    $tmp= array_slice($row, $columnKey, 1);   
                    $tmp= (is_array($tmp) && !empty($tmp))?current($tmp):null;   
                }else{   
                    $tmp= isset($row[$columnKey])?$row[$columnKey]:null;   
                }   
                if(!$indexKeyIsNull){   
                    if($indexKeyIsNumber){   
                      $key = array_slice($row, $indexKey, 1);   
                      $key = (is_array($key) && !empty($key))?current($key):null;   
                      $key = is_null($key)?0:$key;   
                    }else{   
                      $key = isset($row[$indexKey])?$row[$indexKey]:0;   
                    }   
                }   
                $result[$key] = $tmp;   
            }   
            return $result;   
        }else{  
            return array_column($input, $columnKey, $indexKey);  
        }  
    } 

    public function order_teamer(){
		$topkey = $this->visitor->get('topkey');

                $pid = (isset($_GET['bid'])) ? $_GET['bid'] : $_GET[pid] ;
                 $tid = $_GET[tid];
                $this->assign('pid',$pid);
                $this->assign('tid',$tid);
               $pid= unserialize($pid);
		/**************************************************** 订单列表 start **************************************************************/
//		$map['u.topkey'] = $topkey;
                $map['u.id'] = array('IN',$pid);
		$map['u.tgroup_'.$this->user_tgroup] = $this->uid;
		$map['o.lottery'] = array('IN',array(1,2,9));

		$map['o.type'] = 0;
		($id = $this->_get('id', 'intval',0)) && $map['u.id'] = $id;
		($lottery = $this->_get('lottery', 'intval',0)) && $map['o.lottery'] = $lottery;
		($tele = $this->_get('tele', 'trim','')) && $map['u.tele'] = array('like','%'.$tele.'%');

		($map_stime = $this->_request('stime', 'trim')) && $map['o.add_time'][] = array('egt', $map_stime);
        ($map_etime = $this->_request('etime', 'trim')) && $map['o.add_time'][] = array('elt', $map_etime);


		($tgroup_1 = $this->_get('tgroup_1', 'intval',0)) && $map['u.tgroup_1'] = $tgroup_1;
		($tgroup_2 = $this->_get('tgroup_2', 'intval',0)) && $map['u.tgroup_2'] = $tgroup_2;
		($tgroup_3 = $this->_get('tgroup_3', 'intval',0)) && $map['u.tgroup_3'] = $tgroup_3;

		
		$type = $this->_get('type', 'intval',0);
		if($type == 1){
			$map['o.status'] = array('IN',[1,2]);
		}elseif($type == 2){
			$map['o.status'] = array('IN',[9]);
		}elseif($type == 3){
			$map['o.status'] = array('IN',[3,4,5,6]);
		}

		$page_size = 20;
		$count = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($map)->count();
		$pager = new Page($count,$page_size);
		if($this->_request('export') != ''){
			$list = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->limit('0,1000')->where($map)->field('o.prices as prices,o.lottery_total as lottery_total,o.status as status,o.add_time as add_time,o.lottery as lottery,u.id as uid,u.username as username,u.tele as tele,o.id as oid,u.tgroup_1 as tgroup_1,u.tgroup_2 as tgroup_2,u.tgroup_3 as tgroup_3')->order('o.add_time desc')->select();
		}else{
			$list = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->limit($pager->firstRow, $pager->listRows)->where($map)->field('o.prices as prices,o.lottery_total as lottery_total,o.status as status,o.add_time as add_time,o.lottery as lottery,u.id as uid,u.username as username,u.tele as tele,o.id as oid,o.orderid as orderid,u.tgroup_1 as tgroup_1,u.tgroup_2 as tgroup_2,u.tgroup_3 as tgroup_3')->order('o.add_time desc')->select();
		}
		$lottery_arr = array(0=>'待升级',1=>'升级中',2=>'升级成功',9=>'升级失败');
		foreach($list as $key=>$val){
			if(in_array($val['status'],[3,4,5,6])){
				$list[$key]['status'] = '已提货';
			}elseif(in_array($val['status'],[9])){
				$list[$key]['status'] = '退款';
			}else{
				//$list[$key]['status'] = '待提货';
				$list[$key]['status'] = '已提货';
			}
			if($val['lottery'] == 2){
				$list[$key]['prices'] = $val['lottery_total'];
			}
			$list[$key]['lottery'] = $lottery_arr[$val['lottery']];

			$item = D('order_item')->alias('oi')->join(table('item').' as i ON oi.item_id = i.id')->field('i.title as title,i.img as img,oi.nums as nums,oi.skus as skus')->where(array('oi.order_id'=>$val['oid']))->find();
			$list[$key]['title'] = $item['title'];
			$list[$key]['img'] = $item['img'];
			$list[$key]['nums'] = $item['nums'];
			$list[$key]['skus'] = $item['skus'];

			$list[$key]['tgroup_1_name'] = D('user')->where(['id'=>$val['tgroup_1']])->getField('username');
			$list[$key]['tgroup_2_name'] = D('user')->where(['id'=>$val['tgroup_2']])->getField('username');
			$list[$key]['tgroup_3_name'] = D('user')->where(['id'=>$val['tgroup_3']])->getField('username');

		}
		if($this->_request('export') != ''){
                    
			// 引入excel类文件，将订单用excel文件导出来
			Vendor('excelClass.excelclass');
			$excel = new excelClass();
			$excel->echoUserOrderFile('订单'.date('YmdHis').'.xls',$list);
			exit;
		}
		$page = $pager->show();
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('search',array('id'=>$id,'type'=>$type,'lottery'=>$lottery,'stime'=>$map_stime,'etime'=>$map_etime,'tele'=>$tele));
		/**************************************************** 订单列表 end **************************************************************/


		/**************************************************** 统计 start **************************************************************/
		$stime = date('Y-m-d') . ' 04:00:00';
        $etime = date('Y-m-d') . ' 23:59:59';
//		$order_map['u.topkey'] = $topkey;
                 $order_map['u.id'] = array('IN',$pid);
		$order_map['u.tgroup_'.$this->user_tgroup] = $this->uid;
		$order_map['o.lottery'] = array('IN',array(1,2,9));
		$order_map['o.type'] = 0;
		($id = $this->_get('id', 'intval',0)) && $order_map['u.id'] = $id;
		($lottery = $this->_get('lottery', 'intval',0)) && $order_map['o.lottery'] = $lottery;
		($tele = $this->_get('tele', 'trim','')) && $order_map['u.tele'] = array('like','%'.$tele.'%');
		($map_stime = $this->_request('stime', 'trim')) && $order_map['o.add_time'][] = array('egt', $map_stime);
        ($map_etime = $this->_request('etime', 'trim')) && $order_map['o.add_time'][] = array('elt', $map_etime);
		($tgroup_1 = $this->_get('tgroup_1', 'intval',0)) && $order_map['u.tgroup_1'] = $tgroup_1;
		($tgroup_2 = $this->_get('tgroup_2', 'intval',0)) && $order_map['u.tgroup_2'] = $tgroup_2;
		($tgroup_3 = $this->_get('tgroup_3', 'intval',0)) && $order_map['u.tgroup_3'] = $tgroup_3;

		//订单总金额
		$order['total'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($order_map)->sum('prices');
		$arr=  D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->field('u.id,u.weixin')->where($order_map)->order('u.id desc')->select();
		//升级成功退款金额
		$s_r_map = $s_d_map = $s_w_map = $d_map = $up_map = $order_map;
		$s_r_map['o.status'] = array('IN',[8,9]);
		$s_r_map['o.lottery'] = 2;
		$order['success_refund'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($s_r_map)->sum('lottery_total');
		//升级成功提货金额
		$s_d_map['o.status'] = array('IN',[3,4,5]);
		$s_d_map['o.lottery'] = 2;
		$order['success_delivery'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($s_d_map)->sum('lottery_total');
		//升级成功待处理金额
		$s_w_map['o.status'] = 2;
		$s_w_map['o.lottery'] = 2;
		$order['success_wait'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($s_w_map)->sum('lottery_total');
		//升级失败金额
		$d_map['o.lottery'] = 9;
		$order['failed'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($d_map)->sum('prices');
		//升级中金额
		$up_map['o.lottery'] = 1;
		$order['uporder'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($up_map)->sum('prices');

		$order_map['o.add_time'][] =  array('elt', $etime);
		$order_map['o.add_time'][] =  array('egt', $stime);
		$order['days'] = D('order')->alias('o')->join(table('user').' as u ON o.uid = u.id')->where($order_map)->sum('prices');

		$order['total'] = $order['total'] ? $order['total'] : 0.00;
		$order['success_refund'] = $order['success_refund'] ? $order['success_refund'] : 0.00;
		$order['success_delivery'] = $order['success_delivery'] ? $order['success_delivery'] : 0.00;
		$order['success_wait'] = $order['success_wait'] ? $order['success_wait'] : 0.00;
		$order['failed'] = $order['failed'] ? $order['failed'] : 0.00;
		$order['uporder'] = $order['uporder'] ? $order['uporder'] : 0.00;
		$order['days'] = $order['days'] ? $order['days'] : 0.00;

		$this->assign('order',$order);
		$arr1=[];
		foreach ($arr as $key => $val) {
			$arr1[$key] = $val[id];
		}
		$arr1 = (!empty($arr)) ? $arr1 : Array(0) ;
		$kid=array_unique($arr1);


		$map_['uid'] = array('IN',$kid);
		$map_['action'] = 'commission';

		($map_stime = $this->_request('stime', 'trim')) && $map_['add_time'][] = array('egt', $map_stime);
		($map_etime = $this->_request('etime', 'trim')) && $map_['add_time'][] = array('elt', $map_etime);
		$commission = D('price_log')->where($map_)->sum('price');
		$commission = $commission ? $commission : 0.00;
		$this->assign('commission',$commission);
		$map_['action'] = 'express';
		$express = D('price_log')->where($map_)->sum('price');
		$this->assign('express',$express);
		/**************************************************** 统计 end **************************************************************/

		$this->display();
	}
        

        /* 资金明细 */
	public function price_teamer(){
		$topkey = $this->visitor->get('topkey');
		  $pid = (isset($_GET['bid'])) ? $_GET['bid'] : $_GET[pid] ;
		 $tid = $_GET[tid];
		$this->assign('pid',$pid);
		$this->assign('tid',$tid);
	   $pid= unserialize($pid);

	   $map['u.id'] = array('IN',$pid);
		$map['u.tgroup_'.$this->user_tgroup] = $this->uid;

		($id = $this->_get('id', 'intval',0)) && $map['u.id'] = $id;
		($type = $this->_get('type', 'intval',0)) && $map['ur.type'] = $type;

		($tgroup_1 = $this->_get('tgroup_1', 'intval',0)) && $map['u.tgroup_1'] = $tgroup_1;
		($tgroup_2 = $this->_get('tgroup_2', 'intval',0)) && $map['u.tgroup_2'] = $tgroup_2;
		($tgroup_3 = $this->_get('tgroup_3', 'intval',0)) && $map['u.tgroup_3'] = $tgroup_3;
               //7.19
                if ($_GET['username']) {
		$realname= trim($_GET['username']);
		$map['u.username'] = array('like', '%' . $_GET['username'] . '%');
                $this->assign('username_',$username);
                
		}
                
                 if ($_GET['tele']) {
		$tele= trim($_GET['tele']);
		$map['u.tele'] =$tele;
                $this->assign('tele_',$tele);
                
		}
                
                 if ($_GET['stime']) {
                $time_start = $_GET['stime'];
                $time_end = $_GET['etime'];
                $map['ur.add_time'] = array(array('egt', $time_start), array('elt', $time_end));
                 $this->assign('stime_',$_GET['stime']);
                $this->assign('etime_',$_GET['etime']);
                }
                
		$map['ur.status'] = 1;
		$count = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($map)->count();
		$page_size = 20;
		$pager = new Page($count,$page_size);
		if($this->_request('export') != ''){
			$list = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->field('ur.*,u.tele as tele,u.tgroup_1 as tgroup_1,u.tgroup_2 as tgroup_2,u.tgroup_3 as tgroup_3')->where($map)->select();
			foreach($list as $key=>$val){
				$list[$key]['type'] = $val['type'] == 1 ? '充值' : '提现';
			}
		}else{
			$list = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->field('ur.*,u.tele as tele,u.tgroup_1 as tgroup_1,u.tgroup_2 as tgroup_2,u.tgroup_3 as tgroup_3')->limit($pager->firstRow, $pager->listRows)->where($map)->select();
		}
		foreach($list as $key=>$val){
			$list[$key]['tgroup_1_name'] = D('user')->where(['id'=>$val['tgroup_1']])->getField('username');
			$list[$key]['tgroup_2_name'] = D('user')->where(['id'=>$val['tgroup_2']])->getField('username');
			$list[$key]['tgroup_3_name'] = D('user')->where(['id'=>$val['tgroup_3']])->getField('username');
			$list[$key]['tgroup_4_name'] = D('user')->where(['id'=>$val['tgroup_4']])->getField('username');
			$list[$key]['tgroup_5_name'] = D('user')->where(['id'=>$val['tgroup_5']])->getField('username');
		}
		$page = $pager->show();
		if($this->_request('export') != ''){
			// 引入excel类文件，将订单用excel文件导出来
			Vendor('excelClass.excelclass');
			$excel = new excelClass();
			$excel->echoUserPriceFile('资金'.date('YmdHis').'.xls',$list);
			exit;
		}
		$this->assign('list',$list);
		$this->assign('page',$page);
                //7.20
                $this->assign('search', array(
            'id' =>$id,
            'type' => $type,
            'tele'   => $_GET['tele'],
             'username'   => $_GET['username'],
            'stime'   => $_GET['stime'],
            'etime'   => $_GET['etime'],
        ));


		/* 统计 */
		$stime = date('Y-m-d') . ' 04:00:00';
        $etime = date('Y-m-d') . ' 23:59:59';
//		$recharge_map['u.topkey'] = $topkey;
                $recharge_map['u.id'] = array('IN',$pid);
		($id = $this->_get('id', 'intval',0)) && $recharge_map['u.id'] = $id;
		$recharge_map['u.tgroup_'.$this->user_tgroup] = $this->uid;

		($tgroup_1 = $this->_get('tgroup_1', 'intval',0)) && $recharge_map['u.tgroup_1'] = $tgroup_1;
		($tgroup_2 = $this->_get('tgroup_2', 'intval',0)) && $recharge_map['u.tgroup_2'] = $tgroup_2;
		($tgroup_3 = $this->_get('tgroup_3', 'intval',0)) && $recharge_map['u.tgroup_3'] = $tgroup_3;

		$recharge_map['ur.status'] = 1;
		$recharge_map['ur.type'] = 1;
		$recharge['total'] = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');
		$recharge_map['ur.add_time'][] =  array('elt', $etime);
		$recharge_map['ur.add_time'][] =  array('egt', $stime);
		$recharge['days'] = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');

		$recharge_map['ur.type'] = 2;
		unset($recharge_map['ur.add_time']);
		$cash['total'] = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');
		$recharge_map['ur.add_time'][] =  array('elt', $etime);
		$recharge_map['ur.add_time'][] =  array('egt', $stime);
		$cash['days'] = D('user_recharge')->alias('ur')->join(table('user').' as u ON ur.uid=u.id')->where($recharge_map)->sum('ur.price');

		$recharge['total'] = $recharge['total'] ? $recharge['total'] : 0.00;
		$recharge['days'] = $recharge['days'] ? $recharge['days'] : 0.00;
		$cash['total'] = $cash['total'] ? $cash['total'] : 0.00;
		$cash['days'] = $cash['days'] ? $cash['days'] : 0.00;

		$this->assign('recharge',$recharge);
		$this->assign('cash',$cash);

		$this->display();
	} 
         
}